Monday, June 20, 2011

How to install a Windows service using command line ?

How to install a Windows service using command line ?

1>> Make a build of the service project then go to bin directory and copy required build files from either of the directory Release / Debug.
2>> Service will be in .exe file. This should be installed now, by using a command line   as below –

Copy blow one to bat file , name it as install.bat.

c:
cd c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
InstallUtil.exe "E:\test\testservice.exe"
pause


Run install.bat to install the service.

3> To uninstall there are two ways –
               a)
c:
cd c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
InstallUtil.exe –u "E:\test\testservice.exe"
pause

b)
sc delete <serviceName>
pause

               Some time using point number 3.a  , it is required to restart the windows machine so better to use the point 3.b.



Note- if you struck with the service which is already installed then you can kill that process , either using command line or ctrl+Alt+del select service and kill the process.

Friday, June 17, 2011

How to remove item from a List in a Loop ?

It is not an easy task of removing an item from the list in the loop. By just doing the loop and removing the item we can encounter much complications.

So better way is to loop the list from bottom -> up and remove the item based on the condition.

Sample code piece -



using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            List<string> ls = new List<string>();

            ls.Add("a");
            ls.Add("b");
            ls.Add("c");
            ls.Add("d");
            ls.Add("e");

            for (int i = ls.Count - 1; i >= 0; i--)
            {


                if (ls[i] == "d")
                {
                    Console.WriteLine("del:" + ls[i].ToString());
                    ls.RemoveAt(i);
                }

            }

            for (int i = 0; i < ls.Count; i++)
            {
                Console.WriteLine(ls[i].ToString());
            }

        }
    }
}

Code Formater

Paste Here Your Source Code
Source Code Formatting Options
1) Convert Tab into Space :
2) Need Line Code Numbering :
3) Remove blank lines :
4) Embeded styles / Stylesheet :
5) Code Block Width :
6) Code Block Height :
7) Alternative Background :
Copy Formatted Source Code
 
Preview Of Formatted Code