新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
淺談C#安裝服務(wù)和卸載服務(wù)
這是一個(gè)C#安裝服務(wù)和卸載服務(wù)的類,有興趣可以看一下.

主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站建設(shè)、程序開(kāi)發(fā)、微網(wǎng)站、小程序開(kāi)發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。
- using System;
- using System.Runtime.InteropServices;
- namespace EAE.MyServiceInstaller
- {
- class ServiceInstaller
- {
- #region Private Variables
- private string _servicePath;
- private string _serviceName;
- private string _serviceDisplayName;
- #endregion Private Variables
- #region DLLImport
- [DllImport("advapi32.dll")]
- public static extern IntPtr OpenSCManager(string lpMachineName,string lpSCDB, int scParameter);
- [DllImport("Advapi32.dll")]
- public static extern IntPtr CreateService(IntPtr SC_HANDLE,string lpSvcName,string lpDisplayName,
- int dwDesiredAccess,int dwServiceType,int dwStartType,int dwErrorControl,string lpPathName,
- string lpLoadOrderGroup,int lpdwTagId,string lpDependencies,string lpServiceStartName,string lpPassword);
- [DllImport("advapi32.dll")]
- public static extern void CloseServiceHandle(IntPtr SCHANDLE);
- [DllImport("advapi32.dll")]
- public static extern int StartService(IntPtr SVHANDLE,int dwNumServiceArgs,string lpServiceArgVectors);
- [DllImport("advapi32.dll",SetLastError=true)]
- public static extern IntPtr OpenService(IntPtr SCHANDLE,string lpSvcName,int dwNumServiceArgs);
- [DllImport("advapi32.dll")]
- public static extern int DeleteService(IntPtr SVHANDLE);
- [DllImport("kernel32.dll")]
- public static extern int GetLastError();
- #endregion DLLImport
- // ///
- // /// C#安裝服務(wù)應(yīng)用程序入口.
- // ///
- //
- // [STAThread]
- // static void Main(string[] args)
- // {
- //
- // string svcPath;
- // string svcName;
- // string svcDispName;
- // //C#安裝服務(wù)程序的路徑
- // svcPath = @"d:\service\EAEWS.exe";
- // svcDispName="myEAEWS";
- // svcName= "myEAEWS";
- // ServiceInstaller c = new ServiceInstaller();
- // c.InstallService(svcPath, svcName, svcDispName);
- // Console.Read();
- //
- // }
- ///
- /// 安裝和運(yùn)行
- ///
- /// C#安裝程序路徑.
- /// 服務(wù)名
- /// 服務(wù)顯示名稱.
- /// 服務(wù)安裝是否成功.
- public bool InstallService(string svcPath, string svcName, string svcDispName)
- {
- #region Constants declaration.
- int SC_MANAGER_CREATE_SERVICE = 0x0002;
- int SERVICE_WIN32_OWN_PROCESS = 0x00000010;
- //int SERVICE_DEMAND_START = 0x00000003;
- int SERVICE_ERROR_NORMAL = 0x00000001;
- int STANDARD_RIGHTS_REQUIRED = 0xF0000;
- int SERVICE_QUERY_CONFIG = 0x0001;
- int SERVICE_CHANGE_CONFIG = 0x0002;
- int SERVICE_QUERY_STATUS = 0x0004;
- int SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
- int SERVICE_START =0x0010;
- int SERVICE_STOP =0x0020;
- int SERVICE_PAUSE_CONTINUE =0x0040;
- int SERVICE_INTERROGATE =0x0080;
- int SERVICE_USER_DEFINED_CONTROL =0x0100;
- int SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
- SERVICE_QUERY_CONFIG |
- SERVICE_CHANGE_CONFIG |
- SERVICE_QUERY_STATUS |
- SERVICE_ENUMERATE_DEPENDENTS |
- SERVICE_START |
- SERVICE_STOP |
- SERVICE_PAUSE_CONTINUE |
- SERVICE_INTERROGATE |
- SERVICE_USER_DEFINED_CONTROL);
- int SERVICE_AUTO_START = 0x00000002;
- #endregion Constants declaration.
- try
- {
- IntPtr sc_handle = OpenSCManager(null,null,SC_MANAGER_CREATE_SERVICE);
- if (sc_handle.ToInt32() != 0)
- {
- IntPtr sv_handle = CreateService(sc_handle,svcName,svcDispName,SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,svcPath,null,0,null,null,null);
- if(sv_handle.ToInt32() ==0)
- {
- CloseServiceHandle(sc_handle);
- return false;
- }
- else
- {
- //試嘗啟動(dòng)服務(wù)
- int i = StartService(sv_handle,0,null);
- if(i==0)
- {
- return false;
- }
- CloseServiceHandle(sc_handle);
- return true;
- }
- }
- else
- return false;
- }
- catch(Exception e)
- {
- throw e;
- }
- }
- ///
- /// 反安裝服務(wù).
- ///
- /// 服務(wù)名.
- public bool UnInstallService(string svcName)
- {
- int GENERIC_WRITE = 0x40000000;
- IntPtr sc_hndl = OpenSCManager(null,null,GENERIC_WRITE);
- if(sc_hndl.ToInt32() !=0)
- {
- int DELETE = 0x10000;
- IntPtr svc_hndl = OpenService(sc_hndl,svcName,DELETE);
- if(svc_hndl.ToInt32() !=0)
- {
- int i = DeleteService(svc_hndl);
- if (i != 0)
- {
- CloseServiceHandle(sc_hndl);
- return true;
- }
- else
- {
- CloseServiceHandle(sc_hndl);
- return false;
- }
- }
- else
- return false;
- }
- else
- return false;
- }
- }
- }
C#安裝服務(wù)和卸載服務(wù)就介紹到這里。
【編輯推薦】
- C#枚舉類型使用的一點(diǎn)總結(jié)
- C#枚舉文件的代碼實(shí)現(xiàn)
- C# 操作Excel實(shí)例淺析
- C# 操作Excel之動(dòng)態(tài)創(chuàng)建淺析
- C# 操作Excel之Delphi控件方法
文章名稱:淺談C#安裝服務(wù)和卸載服務(wù)
網(wǎng)頁(yè)網(wǎng)址:http://www.dlmjj.cn/article/ccospgp.html


咨詢
建站咨詢
