利用C#建立和安裝一個windows服務

   最近項目須要,須要定時獲取天氣資料往數據數庫內寫入數據,因此就考慮到了.net內的window服務。之前沒有這方面的需求,因此基本沒怎麼接觸過。因此也藉此次機會好好補充下這方面的知識,以備之後工做之需。javascript

  關於winds服務的介紹,這裏有一篇文章介紹得很清楚:http://blog.csdn.net/yysyangyangyangshan/article/details/7295739,但這裏的具體步驟講述不是很清楚,因此現用具體的方式再講述下windows服務的開發與安裝事項。html

開發環境:Win7 32位;工具:visualstudio2010。 由於win7自帶的就有.net環境,算是偷一下懶吧。由於不管是手動安裝或程序安裝都要用到。一個目錄(默認C盤爲操做系統的狀況):C:\Windows\Microsoft.NET\Framework,若是你的代碼是.net2.0:C:\Windows\Microsoft.NET\Framework\v2.0.50727;4.0:C:\Windows\Microsoft.NET\Framework\v4.0.30319。 下面看一下代碼: 1、建立windows服務 如圖新建一個Windows服務 進入程序如圖 空白服務以下java

[csharp] view plain copy print ?
  1. public partial class Service1 : ServiceBase  
  2.    {  
  3.        System.Threading.Timer recordTimer;  
  4.   
  5.   
  6.        public Service1()  
  7.        {  
  8.            InitializeComponent();  
  9.        }  
  10.   
  11.   
  12.        protected override void OnStart(string[] args)  
  13.        {  
  14.        }  
  15.   
  16.   
  17.        protected override void OnStop()  
  18.        {  
  19.        }  
  20.    }  
 public partial class Service1 : ServiceBase
    {
        System.Threading.Timer recordTimer;


        public Service1()
        {
            InitializeComponent();
        }


        protected override void OnStart(string[] args)
        {
        }


        protected override void OnStop()
        {
        }
    }

只要在OnStart裏完成你的功能代碼便可。本例中咱們作一個定時向本地文件寫記錄的功能。 如圖 建立一個類,用戶寫文件,windows

[csharp] view plain copy print ?
  1. public class FileOpetation  
  2.    {  
  3.        /// <summary>  
  4.        /// 保存至本地文件  
  5.        /// </summary>  
  6.        /// <param name="ETMID"></param>  
  7.        /// <param name="content"></param>  
  8.        public static void SaveRecord(string content)  
  9.        {  
  10.            if (string.IsNullOrEmpty(content))  
  11.            {  
  12.                return;  
  13.            }  
  14.   
  15.   
  16.            FileStream fileStream = null;  
  17.   
  18.   
  19.            StreamWriter streamWriter = null;  
  20.   
  21.   
  22.            try  
  23.            {  
  24.                string path = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, string.Format("{0:yyyyMMdd}", DateTime.Now));  
  25.   
  26.   
  27.                using (fileStream = new FileStream(path, FileMode.Append, FileAccess.Write))  
  28.                {  
  29.                    using (streamWriter = new StreamWriter(fileStream))  
  30.                    {  
  31.                        streamWriter.Write(content);  
  32.   
  33.   
  34.                        if (streamWriter != null)  
  35.                        {  
  36.                            streamWriter.Close();  
  37.                        }  
  38.                    }  
  39.   
  40.   
  41.                    if (fileStream != null)  
  42.                    {  
  43.                        fileStream.Close();  
  44.                    }  
  45.                }  
  46.            }  
  47.            catch { }  
  48.        }  
  49.    }  
 public class FileOpetation
    {
        /// <summary>
        /// 保存至本地文件
        /// </summary>
        /// <param name="ETMID"></param>
        /// <param name="content"></param>
        public static void SaveRecord(string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                return;
            }


            FileStream fileStream = null;


            StreamWriter streamWriter = null;


            try
            {
                string path = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, string.Format("{0:yyyyMMdd}", DateTime.Now));


                using (fileStream = new FileStream(path, FileMode.Append, FileAccess.Write))
                {
                    using (streamWriter = new StreamWriter(fileStream))
                    {
                        streamWriter.Write(content);


                        if (streamWriter != null)
                        {
                            streamWriter.Close();
                        }
                    }


                    if (fileStream != null)
                    {
                        fileStream.Close();
                    }
                }
            }
            catch { }
        }
    }

那麼在Service1中調用,ide

[csharp] view plain copy print ?
  1. public partial class Service1 : ServiceBase  
  2.    {  
  3.        System.Threading.Timer recordTimer;  
  4.   
  5.   
  6.        public Service1()  
  7.        {  
  8.            InitializeComponent();  
  9.        }  
  10.   
  11.   
  12.        protected override void OnStart(string[] args)  
  13.        {  
  14.            IntialSaveRecord();  
  15.        }  
  16.   
  17.   
  18.        protected override void OnStop()  
  19.        {  
  20.            if (recordTimer != null)  
  21.            {  
  22.                recordTimer.Dispose();  
  23.            }  
  24.        }  
  25.   
  26.   
  27.        private void IntialSaveRecord()  
  28.        {  
  29.            TimerCallback timerCallback = new TimerCallback(CallbackTask);  
  30.   
  31.   
  32.            AutoResetEvent autoEvent = new AutoResetEvent(false);  
  33.   
  34.   
  35.            recordTimer = new System.Threading.Timer(timerCallback, autoEvent, 10000, 60000 * 10);  
  36.        }  
  37.   
  38.   
  39.        private void CallbackTask(Object stateInfo)  
  40.        {  
  41.            FileOpetation.SaveRecord(string.Format(@"當前記錄時間:{0},情況:程序運行正常!", DateTime.Now));  
  42.        }  
  43.    }  
 public partial class Service1 : ServiceBase
    {
        System.Threading.Timer recordTimer;


        public Service1()
        {
            InitializeComponent();
        }


        protected override void OnStart(string[] args)
        {
            IntialSaveRecord();
        }


        protected override void OnStop()
        {
            if (recordTimer != null)
            {
                recordTimer.Dispose();
            }
        }


        private void IntialSaveRecord()
        {
            TimerCallback timerCallback = new TimerCallback(CallbackTask);


            AutoResetEvent autoEvent = new AutoResetEvent(false);


            recordTimer = new System.Threading.Timer(timerCallback, autoEvent, 10000, 60000 * 10);
        }


        private void CallbackTask(Object stateInfo)
        {
            FileOpetation.SaveRecord(string.Format(@"當前記錄時間:{0},情況:程序運行正常!", DateTime.Now));
        }
    }

這樣服務算是寫的差很少了,下面添加一個安裝類,用於安裝。 如圖,在service1上右鍵-添加安裝程序, 如圖,添加一個安裝程序, 如圖,添加完成後, 設置相應的屬性,給serviceInstaller1設置屬性,主要是描述信息。如圖, 給serviceProcessInstaller1設置,主要是account。通常選localsystem,如圖, 這樣服務已經寫好了。那麼如何添加到windows服務裏面去呢。除了以前說過的用CMD,InstallUtil.exe和服務的exe文件進行手動添加。這些能夠用代碼來實現的。固然主要過程都是同樣的。代碼實現也是使用dos命令來完成的。 2、代碼安裝Windows服務 上面寫好的服務,最終生成的是一個exe文件。如圖, 安裝程序安裝時須要用到這個exe的路徑,因此方便起見,將這個生成的exe文件拷貝至安裝程序的運行目錄下。工具

安裝代碼, 字體

 

[csharp] view plain copy print ?
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.              Application.EnableVisualStyles();  
  6.   
  7.   
  8.             Application.SetCompatibleTextRenderingDefault(false);  
  9.   
  10.   
  11.             string sysDisk = System.Environment.SystemDirectory.Substring(0,3);  
  12.   
  13.   
  14.             string dotNetPath = sysDisk + @"WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe";//由於當前用的是4.0的環境  
  15.   
  16.   
  17.             string serviceEXEPath = Application.StartupPath+@"\MyFirstWindowsService.exe";//把服務的exe程序拷貝到了當前運行目錄下,因此用此路徑  
  18.   
  19.   
  20.             string serviceInstallCommand = string.Format(@"{0}  {1}", dotNetPath, serviceEXEPath);//安裝服務時使用的dos命令  
  21.   
  22.   
  23.             string serviceUninstallCommand = string.Format(@"{0} -U {1}", dotNetPath, serviceEXEPath);//卸載服務時使用的dos命令  
  24.   
  25.   
  26.             try  
  27.             {  
  28.                 if (File.Exists(dotNetPath))  
  29.                 {  
  30.                     string[] cmd = new string[] { serviceUninstallCommand };  
  31.   
  32.   
  33.                     string ss = Cmd(cmd);  
  34.   
  35.   
  36.                     CloseProcess("cmd.exe");  
  37.                 }  
  38.   
  39.   
  40.             }  
  41.             catch  
  42.             {  
  43.             }  
  44.   
  45.   
  46.             Thread.Sleep(1000);  
  47.   
  48.   
  49.             try  
  50.             {  
  51.                 if (File.Exists(dotNetPath))  
  52.                 {  
  53.                     string[] cmd = new string[] { serviceInstallCommand };  
  54.   
  55.   
  56.                     string ss = Cmd(cmd);  
  57.   
  58.   
  59.                     CloseProcess("cmd.exe");  
  60.                 }  
  61.   
  62.   
  63.             }  
  64.             catch  
  65.             {  
  66.   
  67.   
  68.             }  
  69.   
  70.   
  71.             try  
  72.             {  
  73.                 Thread.Sleep(3000);  
  74.   
  75.   
  76.                 ServiceController sc = new ServiceController("MyFirstWindowsService");  
  77.   
  78.   
  79.                 if (sc != null && (sc.Status.Equals(ServiceControllerStatus.Stopped)) ||  
  80.   
  81.   
  82.                           (sc.Status.Equals(ServiceControllerStatus.StopPending)))  
  83.                 {  
  84.                     sc.Start();  
  85.                 }  
  86.                 sc.Refresh();  
  87.             }  
  88.             catch  
  89.             {  
  90.             }  
  91.         }  
  92.   
  93.   
  94.         /// <summary>  
  95.         /// 運行CMD命令  
  96.         /// </summary>  
  97.         /// <param name="cmd">命令</param>  
  98.         /// <returns></returns>  
  99.         public static string Cmd(string[] cmd)  
  100.         {  
  101.             Process p = new Process();  
  102.             p.StartInfo.FileName = "cmd.exe";  
  103.             p.StartInfo.UseShellExecute = false;  
  104.             p.StartInfo.RedirectStandardInput = true;  
  105.             p.StartInfo.RedirectStandardOutput = true;  
  106.             p.StartInfo.RedirectStandardError = true;  
  107.             p.StartInfo.CreateNoWindow = true;  
  108.             p.Start();  
  109.             p.StandardInput.AutoFlush = true;  
  110.             for (int i = 0; i < cmd.Length; i++)  
  111.             {  
  112.                 p.StandardInput.WriteLine(cmd[i].ToString());  
  113.             }  
  114.             p.StandardInput.WriteLine("exit");  
  115.             string strRst = p.StandardOutput.ReadToEnd();  
  116.             p.WaitForExit();  
  117.             p.Close();  
  118.             return strRst;  
  119.         }  
  120.   
  121.   
  122.         /// <summary>  
  123.         /// 關閉進程  
  124.         /// </summary>  
  125.         /// <param name="ProcName">進程名稱</param>  
  126.         /// <returns></returns>  
  127.         public static bool CloseProcess(string ProcName)  
  128.         {  
  129.             bool result = false;  
  130.             System.Collections.ArrayList procList = new System.Collections.ArrayList();  
  131.             string tempName = "";  
  132.             int begpos;  
  133.             int endpos;  
  134.             foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())  
  135.             {  
  136.                 tempName = thisProc.ToString();  
  137.                 begpos = tempName.IndexOf("(") + 1;  
  138.                 endpos = tempName.IndexOf(")");  
  139.                 tempName = tempName.Substring(begpos, endpos - begpos);  
  140.                 procList.Add(tempName);  
  141.                 if (tempName == ProcName)  
  142.                 {  
  143.                     if (!thisProc.CloseMainWindow())  
  144.                         thisProc.Kill(); // 當發送關閉窗口命令無效時強行結束進程  
  145.                     result = true;  
  146.                 }  
  147.             }  
  148.             return result;  
  149.         }  
  150.     }  
class Program
    {
        static void Main(string[] args)
        {
             Application.EnableVisualStyles();


            Application.SetCompatibleTextRenderingDefault(false);


            string sysDisk = System.Environment.SystemDirectory.Substring(0,3);


            string dotNetPath = sysDisk + @"WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe";//由於當前用的是4.0的環境


            string serviceEXEPath = Application.StartupPath+@"\MyFirstWindowsService.exe";//把服務的exe程序拷貝到了當前運行目錄下,因此用此路徑


            string serviceInstallCommand = string.Format(@"{0}  {1}", dotNetPath, serviceEXEPath);//安裝服務時使用的dos命令


            string serviceUninstallCommand = string.Format(@"{0} -U {1}", dotNetPath, serviceEXEPath);//卸載服務時使用的dos命令


            try
            {
                if (File.Exists(dotNetPath))
                {
                    string[] cmd = new string[] { serviceUninstallCommand };


                    string ss = Cmd(cmd);


                    CloseProcess("cmd.exe");
                }


            }
            catch
            {
            }


            Thread.Sleep(1000);


            try
            {
                if (File.Exists(dotNetPath))
                {
                    string[] cmd = new string[] { serviceInstallCommand };


                    string ss = Cmd(cmd);


                    CloseProcess("cmd.exe");
                }


            }
            catch
            {


            }


            try
            {
                Thread.Sleep(3000);


                ServiceController sc = new ServiceController("MyFirstWindowsService");


                if (sc != null && (sc.Status.Equals(ServiceControllerStatus.Stopped)) ||


                          (sc.Status.Equals(ServiceControllerStatus.StopPending)))
                {
                    sc.Start();
                }
                sc.Refresh();
            }
            catch
            {
            }
        }


        /// <summary>
        /// 運行CMD命令
        /// </summary>
        /// <param name="cmd">命令</param>
        /// <returns></returns>
        public static string Cmd(string[] cmd)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.AutoFlush = true;
            for (int i = 0; i < cmd.Length; i++)
            {
                p.StandardInput.WriteLine(cmd[i].ToString());
            }
            p.StandardInput.WriteLine("exit");
            string strRst = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            return strRst;
        }


        /// <summary>
        /// 關閉進程
        /// </summary>
        /// <param name="ProcName">進程名稱</param>
        /// <returns></returns>
        public static bool CloseProcess(string ProcName)
        {
            bool result = false;
            System.Collections.ArrayList procList = new System.Collections.ArrayList();
            string tempName = "";
            int begpos;
            int endpos;
            foreach (System.Diagnostics.Process thisProc in System.Diagnostics.Process.GetProcesses())
            {
                tempName = thisProc.ToString();
                begpos = tempName.IndexOf("(") + 1;
                endpos = tempName.IndexOf(")");
                tempName = tempName.Substring(begpos, endpos - begpos);
                procList.Add(tempName);
                if (tempName == ProcName)
                {
                    if (!thisProc.CloseMainWindow())
                        thisProc.Kill(); // 當發送關閉窗口命令無效時強行結束進程
                    result = true;
                }
            }
            return result;
        }
    }

這段代碼其實能夠放在項目中的某個地方,或單獨執行程序中,只好設置好dotNetPath和serviceEXEPath路徑就能夠了。this

 

運行完後,如圖, 再在安裝目錄下看記錄的文件,spa

這樣,一個windows服務安裝成功了。操作系統

代碼下載:http://download.csdn.net/detail/yysyangyangyangshan/6032671

 

 

原文地址:http://blog.csdn.net/yysyangyangyangshan/article/details/10515035

 

再另加windows服務的具體安裝步驟:

安裝服務

點擊 開始,運行中輸入cmd,獲取命令提示符

win7須要已管理員的身份啓動,不然沒法安裝

  • 輸入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回車

    切換當前目錄,此處須要注意的是,在C:\Windows\Microsoft.NET\Framework目錄下有不少相似版本,具體去哪一個目錄要看項目的運行環境,例 若是是.net framework2.0則須要輸入 cd C:\Windows\Microsoft.NET\Framework\v2.0.50727

  • 輸入 InstallUtil.exe E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe 回車

     

     

    說明:E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe表示項目生成的exe文件位置

  • 打開服務,就能夠看到已經安裝的服務了

    END

百度經驗:jingyan.baidu.com

卸載服務

  1. 1

    卸載很簡單,打開cmd, 直接輸入 sc delete WinServiceTest即可

     

     

    其餘安裝方式:

    裝Winfows服務首先要添加安裝程序,添加安裝程序步驟以下:

    一、將Windows服務程序切換到設計視圖, 右擊設計視圖選擇「添加安裝程序」

    二、切換到剛被添加的ProjectInstaller的設計視圖

    通常設置以下:

     設置serviceInstaller1組件的屬性:     1) ServiceName = 服務名稱     2) StartType = Automatic ,即自動  設置serviceProcessInstaller1組件的屬性     1) Account = LocalSystem,帳戶通常設置爲本地系統

    三、生成解決方案

     

    安裝服務:

    方法1、使用DOS命令安裝window服務

    一、在服務所在的文件夾下的bin\debug文件夾下找到.exe文件(例如WindowsService1.exe)

    將此文件拷貝到你想安裝的文件夾中。

    二、進入DOS界面

    (VS2008-->Visual Studio Tools-->Visual Studio 2008 命令提示)來進入DOS,直接用cmd可能有些命令找不到;

    三、輸入

    方法2、使用安裝項目安裝windows服務

    我的比較推薦這個方法,選擇目錄安裝更靈活,並且不用在DOS環境下運行。

    由於本人比較懶,直接給出別人總結的地址

    http://blog.csdn.net/dyzcode/article/details/6981547

    注意,之後每次服務項目有更改的時候,須要編譯服務後,在安裝項目中刷新依賴項!!!

    方法3、

    ProjectInstaller.cs的後臺代碼中添加安裝服務和卸載服務的代碼

     

    [csharp] view plain copy print ?
    1. /// <summary>  
    2. /// 安裝服務  
    3. /// </summary>  
    4. /// <param name="stateSaver"></param>  
    5. public override void Install(System.Collections.IDictionary stateSaver)  
    6. {  
    7.     Microsoft.Win32.RegistryKey system,  
    8.         //HKEY_LOCAL_MACHINE\Services\CurrentControlSet  
    9.         currentControlSet,  
    10.         //...\Services  
    11.         services,  
    12.         //...\<Service Name>  
    13.         service,  
    14.         //...\Parameters - this is where you can put service-specific configuration  
    15.         config;  
    16.   
    17.     try  
    18.     {  
    19.         //Let the project installer do its job  
    20.         base.Install(stateSaver);  
    21.   
    22.         //Open the HKEY_LOCAL_MACHINE\SYSTEM key  
    23.         system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");  
    24.         //Open CurrentControlSet  
    25.         currentControlSet = system.OpenSubKey("CurrentControlSet");  
    26.         //Go to the services key  
    27.         services = currentControlSet.OpenSubKey("Services");  
    28.         //Open the key for your service, and allow writing  
    29.         service = services.OpenSubKey(conServiceName, true);  
    30.         //Add your service's description as a REG_SZ value named "Description"  
    31.         service.SetValue("Description", "<span style="font-family: KaiTi_GB2312;">描述語言</span>");  
    32.         //(Optional) Add some custom information your service will use...  
    33.         config = service.CreateSubKey("Parameters");  
    34.     }  
    35.     catch (Exception e)  
    36.     {  
    37.         Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());  
    38.     }  
    39. }  
    40.   
    41. /// <summary>  
    42. /// 卸載服務  
    43. /// </summary>  
    44. /// <param name="savedState"></param>  
    45. public override void Uninstall(System.Collections.IDictionary savedState)  
    46. {  
    47.     Microsoft.Win32.RegistryKey system,  
    48.         currentControlSet,  
    49.         services,  
    50.         service;  
    51.   
    52.     try  
    53.     {  
    54.         //Drill down to the service key and open it with write permission  
    55.         system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");  
    56.         currentControlSet = system.OpenSubKey("CurrentControlSet");  
    57.         services = currentControlSet.OpenSubKey("Services");  
    58.         service = services.OpenSubKey(conServiceName, true);  
    59.         //Delete any keys you created during installation (or that your service created)  
    60.         service.DeleteSubKeyTree("Parameters");  
    61.         //...  
    62.     }  
    63.     catch (Exception e)  
    64.     {  
    65.         Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());  
    66.     }  
    67.     finally  
    68.     {  
    69.         //Let the project installer do its job  
    70.         base.Uninstall(savedState);  
    71.     }  
    72. }  
            /// <summary>
            /// 安裝服務
            /// </summary>
            /// <param name="stateSaver"></param>
            public override void Install(System.Collections.IDictionary stateSaver)
            {
                Microsoft.Win32.RegistryKey system,
                    //HKEY_LOCAL_MACHINE\Services\CurrentControlSet
                    currentControlSet,
                    //...\Services
                    services,
                    //...\<Service Name>
                    service,
                    //...\Parameters - this is where you can put service-specific configuration
                    config;
    
                try
                {
                    //Let the project installer do its job
                    base.Install(stateSaver);
    
                    //Open the HKEY_LOCAL_MACHINE\SYSTEM key
                    system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
                    //Open CurrentControlSet
                    currentControlSet = system.OpenSubKey("CurrentControlSet");
                    //Go to the services key
                    services = currentControlSet.OpenSubKey("Services");
                    //Open the key for your service, and allow writing
                    service = services.OpenSubKey(conServiceName, true);
                    //Add your service's description as a REG_SZ value named "Description"
                    service.SetValue("Description", "描述語言");
                    //(Optional) Add some custom information your service will use...
                    config = service.CreateSubKey("Parameters");
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
                }
            }
    
            /// <summary>
            /// 卸載服務
            /// </summary>
            /// <param name="savedState"></param>
            public override void Uninstall(System.Collections.IDictionary savedState)
            {
                Microsoft.Win32.RegistryKey system,
                    currentControlSet,
                    services,
                    service;
    
                try
                {
                    //Drill down to the service key and open it with write permission
                    system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
                    currentControlSet = system.OpenSubKey("CurrentControlSet");
                    services = currentControlSet.OpenSubKey("Services");
                    service = services.OpenSubKey(conServiceName, true);
                    //Delete any keys you created during installation (or that your service created)
                    service.DeleteSubKeyTree("Parameters");
                    //...
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
                }
                finally
                {
                    //Let the project installer do its job
                    base.Uninstall(savedState);
                }
            }

    代碼添加完成後

     

    添加window service安裝的批處理命令

    1)在項目添加一個文本文件,改名爲install.bat,編輯文件的內容以下:

    @echo off C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -i "WindowsService1.exe" @pause

    2)在項目添加一個文本文件,改名爲uninstall.bat,編輯文件的內容以下

    @echo off C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u "WindowsService1.exe" @pause

    明:上面綠色字體爲服務名稱

    編譯完成後將debug的文件拷貝到想安裝的目錄下,點擊install.bat即完成安裝。

相關文章
相關標籤/搜索