The place where stuff Happens

Simple Note Pad Writer For Touch C# Part 2

Now we need to set the functionality for the keyboard. We Start by adding a new class to our application. I named mine TextboxUpdate.cs. I removed the default class setting and change them to the following four and also made my class to inherit from Textwriter class.   For editing the a textbox we need an actual textbox and a string to input in the keyboard. Its best to use a string builder because a normal string uses lots of memory that we actual don't need. So I added two variable TextBox control and StringBuilder builder and the set the control with the parameter and also added an event handler.    I set this event although not necessary needed, so when a handle(or pointer) is created the class will check if the builder is empty and if true it will append any text in builder to the textbox and the set it to null. Now, Lets create a method that will append the string to the textbox. In this method will check whether the builder is empty if not, the method will append the stringbuilder to the textbox. After this check it will append any text that was passed through from the parameter to the textbox.   For the actual writing we need an other method that is found in TextWriter and that has varies input, but we are only going to use the one that accept a string a parameter. The method is void Write(string s) and this method (overridden) will check if a handle is set to the textbox and if so it will append the text or if not it will buffertext to the builder.   That method will take care of any characters. For the backspace, I added a method that when it triggered it will cut all the text to a string  and by using string.substring , removed the last character and append the remaining string to textbox. For the enter or return key, I used the method Write and added Environment.NewLine which creates a new line in a string.   This enough for the application to have all the function needed and can even discard any data concerned with stringbuilder because those not use it. I still explained parts because I will posting a continuation of the class.  // //

Simple Note Pad Writer For Touch C# Part 1

There is a big craze over the tablet and I am one that likes the technology and would to  be part of. It took some time to think on what I should do so I decided I would write a small program to test. You don't need to buy a tablet, but it a good thing to have if you wish to progress on such technology. The best think for windows, when press once on the screen is the same when clicking once with the mouse so therefore I created a small keyboard with buttons to simulate the keyboard and added a Text Box to show my text that I typed in. For the Textbox to act like this application we need to set the following properties. Name : mainTextBox BackColor: White Cursor: Default Enabled: False Multiline: True (This property is important for the text box to accept more than one line. We need to set to boolean for the capslock and shift button.   Now we start setting the Capslock function. The method I wrote is when the capslock is clicked all the character(text on the button) will change to uppercase and if clicked again they will change back to lowercase. I also change the appearance of the capslock to change to red when its on.   Both shift buttons work the same and they change state after each click. With a simple if function in each click.   // //

Creating a simple windows service in C#. Part 3

SimpleService.rar (26.75 kb) This is the final part is on how to install our service to the system if you are using Windows XP you can do this by Opening CMD prompt finding the directory (debug folder in your project) using CD (change directory) and then type installutil SimpleService.exe. To uninstall the service go to the directory by using the command prompt and then type installutil -u SimpleService.exe. This method will not work with Windows 7 and some may not be able to install it if you did not install Visual Studio 2010. If you have visual studio 2010, the IDE should have install the tools one of which is Visual Studio 2010 Command Prompt. This found In Visual Studio 2010 > Visual Studio Tools >  Visual Studio Command Prompt (2010) don't forget to run as Administrator or you may not have enough privileges to install the service. Like the command prompt go to the project directory (debug folder in you project) and the type installutil SimpleService.exe If successful you should have a screen like so... Can found in services manager... by accessing administrative tools in your control panel or simple writing services in start menu... To uninstall the service go to the directory by using the  Visual Studio Command Prompt (2010)  and then type installutil -u SimpleService.exe. Don't forget always as an administrator.   // //

Creating a simple windows service in C#. Part 2

SimpleService.rar (26.75 kb)   In the first part we did the service skeleton so now we a custom installer for it... Start by creating a separate class. I name my MyServiceInstaller.cs and add the following reference. using System; using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess; We also need to add [RunInstaller(true)] that will enable the system to use InstallUtil.exe that is great tool that will install services to windows and inherit from the Installer class which we will use to make the installer. In the constructor we need to add two objects for the system to install the service one is ServiceProcessInstaller that handles where the the process of the installation and the other one is ServiceInstaller that install the service. Now we set the privileges in the process installer. We set to account Local System. There a four settings that can be set with ServiceAccount LocalSystem : the service will have full priveleges, but most services do not require such. LocalService : acts like a non-priveleged user, as if anoymous NetworkService : provides extensive local priveleges , provide the credentials to remote servers User : any account choosen by the user. Then we set some information regarding the service these can left out but I recommend that we set them. The display name is to set how it will shown up when you research And start type is to set the start up to automatic or manual or even disable.   Now we tell the installer what to install, by setting the exact of the service which is MySimpleService and finally add both installers to installers list which is in base class.   // //

Creating a simple windows service in C#. Part 1

SimpleService.rar (12.12 kb) Hey, I was researching on services and on how to create them in C# . and this is what I learnt... First we start by create an empty project in visual studio.. To work with services we need to add to references to the project, the System.ServiceProcess and System.Configuration.Install that are found under .NET tab. System.ServiceProcess provides all classes that allow to implement,install and control windows services. System.Configuration.Install provides classes that allow the developer to write custom installers for components.   To add a reference to a project , right click on the project and click on add reference. And windows will pop up. Go the .NET tab and find the reference and double click on it to added to the project. Now create a class and I named mine MyService.cs and start by adding the required using. Using System; Using System.Diagnostics; Using System.ServiceProcess;  and added the base class reference for the ServiceBase. Like so.. In the constructor we set the name for us to identify in the system, we also use the constructor for Initialization code and we need the main method to tell the system to enter in the constructor.   And then we just add two methods onStart and onStop. onStart this is where the service will enter so all methods and code need to be accessed from here. onStop this is where the service will go when the service will stop, so this is the best place where to stop any thread or variables.   // //

Constantly Checking Internet connection C# (part 2 of 2)

To continue, Now we need to create the Event object that was set with the method in the constructor and Method was set to check if the event is required to be invoked or else continue and then checks if the was an error if no error the thread has finished. Therefore in our situation the connection of the Internet has been lost, and show a message box that tells the user the news. I also added a call to set the thread with a new method because you cannot reuse the same thread. Now, all the hard work is ready now what's left is the event handler that was called from the method of the thread.(threadentrypoint). When this is called by the thread it will start the above method. And there the application is ready. And should end up with something like this.     // // Internet.rar (44.50 kb)

Constantly Checking Internet connection C# (part 1 of 2)

Hey, ever wondered how to create a program to check the Internet on the background and resend to the front end that internet connection was lost. I managed to design a way how to do so by Using Threading and AsyncCompletedEventArgs and wrote a small program that start checking the internet connection and then just a simple button with some information to show that you can do something else while the thread is checking. As always when design a GUI Windows Application first I put a button in a from and double clicked to get the method a set it with some information to show to the user.   This is the Method I did to show the User something when he/she click the button.   Now then, to core of the program we start by creating a Private thread in the for a set it with a method and create an event. Then just start the thread. The last method threadEntryPoint() is where all the Checking happens and finieshed with CheckInternet it will throw the event OnThread1Completed that will be explian later in the post. The Method CheckInternet is from the Previous Post Checking internet connections via wininet.dll with a little change that constantly checks the internet in an infinite loop.   // // Internet.rar (44.50 kb)

C# Getting the Local IP and the Public IP

Hey, in a Network related application it a known that to communicate with each other we need IPs. Today i was researching on how to get the local IP and to start we going to use the System.Net this a set of classes related to web communications. First we want to know our host name by using Dns.GetHostName() and converting it to a String. Console.WriteLine("Your Host Name: " + Dns.GetHostName().ToString()); And now we need to get all the local IPs by using Dns.GetHostEntry() like so  IPHostEntry localIPs = Dns.GetHostEntry(Dns.GetHostName()); This method will get all the local IPs including IPv4 and IPv6 and display them. If have you modem installed in you machine and it is connect directly to the internet it will also display the Public IP. foreach (IPAddress ips in localIPs.AddressList) {                          Console.WriteLine(ips.ToString()); } But because I only wanted the IPv4 I had to modify a little bit the so it ignores the IPv6 and display only IPv4 by comparing the IP AddressList with "InterNetwork". If wish to display only the IPv6 instead of IPv4 compare the IP AddressList with "InterNetworkV6". foreach (IPAddress ips in localIPs.AddressList) {     if (ips.AddressFamily.ToString() == "InterNetwork")//If you want the other Version 6 type InterNetworkV6     {            Console.WriteLine("Local IP version 4: " + ips.ToString());     } } Now to get the public IP. There is more ways than one but I am going to show the easiest one yet. The method that I implemented is by getting the IP from an outer source. (From which only display the public ip). N.B We need to be connected to the internet. First we download the who website into the buffer any cause it is quite small I won't take to much. byte[] buffer = new WebClient().DownloadData("");  And the just simply get the the string by encoding the buffer into ASCII and display it like so. Console.WriteLine("Public IP: " + Encoding.ASCII.GetString(buffer));   And the result should be displayed like so. (For security I hid my IPs)         GetIp.rar (20.85 kb)       // //

FTP Research (C#)

Hey, User.. Today I was researching about connecting FTP and C# connection and by default in the Framework (from 2.0 above) has an awesome class that easy to use. The Class is named FtpWebRequest and can be used only by adding System.Net; to the Top of the class. System.IO; needs to be added aswell because we need to move, edit, add file or folders and Functions are in System.IO; The following steps are fundamental to use FTP request executed using FtpWebRequest object: Create an FtpWebRequest object over an ftp server UriFtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(“ftp://” + ftpServerIP + “/”)); Set the ftp method to execute (upload, download, etc.)reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;(Append File, Delete File, Download File, GetDateTimeStamp, GetFileSize, List Directory, List Directory Details, Make Directory, Print Working Directory, Remove Directory, Rename, Upload File, Upload File With Unique Name) Set options(ssl support, transfer as binary/not etc.) for the ftp webrequest. Set the login credentials(username, password)FTP.Credentials = new NetworkCredential(ftpUser, ftpPassword); Execute the request. Recieve the response stream(if required).Mainly used for download. Close the FTP Request, in addition to any open streams.If this is not done another Request cannot start and will throw an exception

Checking internet connections via wininet.dll

The wininet.dll is a inbuilt dll in windows that checks for 6 settings in the Machine and I am going to use in with c# for internet related desktop application INTERNET_CONNECTION_MODEM checks if Local system uses a modem to connect to the Internet. INTERNET_CONNECTION_LAN checks if Local system uses a local area network to connect to the Internet. INTERNET_CONNECTION_PROXY checks if Local system uses a proxy server to connect to the Internet. INTERNET_RAS_INSTALLED checks if Local system has RAS installed. INTERNET_CONNECTION_OFFLINE checks if Local system is in offline mode. INTERNET_CONNECTION_CONFIGURED checks if Local system has a valid connection to the Internet, but it might or might not be currently connected. There is another one that is no longer in use which is INTERNET_CONNECTION_MODEM_BUSY checks if modem uses a modem and if it is busy or not   To use these enums case they are group of enums we need to use two namespaces that are System.Runtime.InteropServices (This Namespace is necessary to call a function exported from unmanaged DLL.) and using System.Data (used for connectionstate).   To use the wininet.dll we need to imported so we let know the machine form when to get some of the methods [DllImport("wininet.dll", CharSet = CharSet.Auto)]     This is the method that checks if the machine is connected to the internet. static extern bool InternetGetConnectedState(ref ConnectionState lpdwFlags, int dwReserved);   The enums that are going to be used to check the machine. [Flags] enum ConnectionState : int //to set lpdwFlags with the following enums {             INTERNET_CONNECTION_MODEM = 0x1,//Local system uses a modem to connect to the Internet.             INTERNET_CONNECTION_LAN = 0x2,//Local system uses a local area network to connect to the Internet.             INTERNET_CONNECTION_PROXY = 0x4,//Local system uses a proxy server to connect to the Internet.             INTERNET_RAS_INSTALLED = 0x10,//Local system is in offline mode.             INTERNET_CONNECTION_OFFLINE = 0x20, //Local system is in offline mode.             INTERNET_CONNECTION_CONFIGURED = 0x40 //Local system has a valid connection to the Internet, but it might or might not be currently connected. }     And in the main method I did a simple checks to get the internet information   static void Main(string[] args) {             ConnectionState Description = 0;               string one= InternetGetConnectedState(ref Description, 0).ToString();             string two = Description.ToString();               Console.WriteLine(one);             Console.WriteLine(two);             Console.ReadLine(); } I also add to the main method to get the description of the connection which is just the names of the enums that are used.   The output should be similar to this if you are connected to the internet InternetConnectionTest.rar (22.46 kb)                 // //