Showing posts with label Dot Net. Show all posts
Showing posts with label Dot Net. Show all posts

Tuesday, September 22, 2009

How to find an element in c# Generic List

The following sample code demonstrates you how to find a matching element in the object list.

Let's say we have an object called Person with public properties PersonID, Name, Age, Gender as defined below.

using System.Collections.Generic.List;

public class Person
{
public int PersonID {get; set;}
public string Name {get; set;}
public int Age {get; set;}
public bool Gender {get; set;}
}

class program
{
public static void Main(string[] args)
{
// Let's say we have a method defined as dbAccess.GetPersons() to
//return all the records from Person table and populates the
//list object "persons" as shown below.
List persons = dbAccess.GetPersons();

int personIDToFind = 1234;
//Find person in persons list by PersonID using predicate
//"p=>p.PersonID == personIDToFind"
Person personByFindOperation = persons.Find( p=>p.PersonID == personIDToFind );

string personName = "Kiran"
//Find person in persons list by Name using predicate
//"p=>p.Name == personName"
Person personByFindOperation2 = persons.Find( p=>p.Name == personName );

}
}

Note: The Find method of List returns the first occurrence of the exact match within the entire System.Collections.Generic.List.


kick it on DotNetKicks.com
Shout it

Thursday, September 10, 2009

How to create a windows service in dot net framework

Windows service is an application which always runs in background. We can view all the installed windows services in ControlPanel/Administrativetools/Services. Sql server, Oracle, ... are the good examples of windows service.

Using visual studio it is very easy to create windows service. Please follow the below steps to create a windows service.
  1. Open visual studio and create a new project of template "Windows Service" as shown below
  2. After you click OK in the above step you will see a new solution named "WindowsService1" created as shown below.
  3. Right click on the above screen and click "Add Installer". This will create an installer component which helps us in installing this service.
  4. You can notice a new component "ProjectInstaller.cs" added and opened as shown below. It has 2 controls "ServiceProcessInstaller1" and "ServiceInstaller1".
  5. You can set whether this service is manual, automatic, or disabled by changing the StartType property of "ServiceInstaller1" (Right click -> Properties).
  6. Change the "ServiceProcessInstaller1"'s property "Account" to "LocalSystem"
  7. Right click on Service1.cs in solution explorer and click "View Code"
  8. In code you will see the service event handlers "OnStart", "OnStop".
  9. Write code that initiates a thread in OnStart event handler.
  10. Write code that stops execution of the thread in OnStop event handler.
  11. Please see below for sample code
  12. Build the solution. You have the windows service built ready.
  13. But until we install this service we cannot see it is running or not.
  14. To install this service you need to open the Visual Studio 2005/2008 Command prompt in Start->Visual Studio 2005/2008->Visual Studio Tools
  15. Run the command : InstallUtil.exe Windows_Service_EXE_Full_Path
  16. Here Windows_Service_EXE_Full_Path is the exe generated for the WindowsService1 project. Let's say the full path of the exe is C:/WindowsService1/Bin/Debug/WindowsService1.exe then our command to install it would be like this.
  17. InstallUtil.exe C:/WindowsService1/Bin/Debug/WindowsService1.exe
  18. If the above command is successful then you will notice a new service named "Service1" is shown in Control Panel->Administrative Tools/Services
  19. To uninstall a service, InstallUtil.exe -u C:/WindowsService1/Bin/Debug/WindowsService1.exe
  20. To make the life easier to install/uninstall windows services we can create a setup project which will let people easily install/uninstall with a wizard Please read this link. http://cherupally.blogspot.com/2009/09/how-to-create-setup-project-to-install.html

kick it on DotNetKicks.com
Shout it

Friday, May 1, 2009

Code to Serialize Object in C# .Net

Please use the below method if you want to serialize any object. This method returns you a string which is XML consists of all public properties and public data members of the object passed. It perfectly works for any type of reference type object.


String SerializeObject(Object obj)
{
try
{
String XmlizedString = null;
MemoryStream memoryStream = new MemoryStream();

XmlSerializer xs = new XmlSerializer(obj.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

xs.Serialize(xmlTextWriter, obj);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
return XmlizedString;
}

catch (Exception e)
{
System.Console.WriteLine(e);
return null;
}
}