設定c#windows服務描述及允許服務與案頭互動的幾種方法(作者部落格還有一大堆C#建立服務的文章)

來源:互聯網
上載者:User

標籤:

方法一:

在ProjectInstaller.cs重寫 install() ,Uninstall()方法


public override void Install(IDictionary stateServer)
  {
   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(stateServer);

    //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(this.serviceInstaller1.ServiceName, true);
    //Add your service‘s description as a REG_SZ value named "Description"
    service.SetValue("Description","PI即時資料採集:能源--每天8點或20點取一次資料;汽車衡--每天1點取一次資料;裝置狀態--每分鐘取一次資料。");
    //(Optional) Add some custom information your service will use...
    //允許服務與案頭互動
    service.SetValue("Type",0x00000110);
    config = service.CreateSubKey("Parameters");
   }
   catch(Exception e)
   {
    Console.WriteLine("An exception was thrown during service installation:/n" + e.ToString());
   }
  }

  public override void Uninstall(IDictionary stateServer)
  {
   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(this.serviceInstaller1.ServiceName, 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(stateServer);
   }
  }

方法二:此方法經測試,發現無效,勾是選上了,但程式啟動後還是沒有介面出現,好像需要電腦重啟才生效

我們寫一個服務,有時候要讓服務啟動某個應用程式,就要修改服務的屬性,勾選允許服務與案頭互動,

可以用修改註冊表實現,我們必須在安裝後操作,所以請重寫Installer的OnAfterInstall。

protected override void OnAfterInstall(System.Collections.IDictionary savedState) {
RegistryKey rk = Registry.LocalMachine;
string key = @"SYSTEM/CurrentControlSet/Services/" + this.sInstaller.ServiceName;
RegistryKey sub = rk.OpenSubKey(key, true);
int value = (int)sub.GetValue("Type");
if (value < 256) {
sub.SetValue("Type", value | 256);
}
base.OnAfterInstall(savedState);
}onstart的時候修改註冊表   
   [HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/你的服務名]   
   "Type"=dword:00000010   
   key    value+256   
   比如現在00000010是16+256=272   
   16進位就是00000110 方法三:使用System.ServiceProcess.ServiceController
            ConnectionOptions coOptions = new ConnectionOptions();            coOptions.Impersonation = ImpersonationLevel.Impersonate;            ManagementScope mgmtScope = new System.Management.ManagementScope(@"root/CIMV2", coOptions);            mgmtScope.Connect();            ManagementObject wmiService;            wmiService = new ManagementObject("Win32_Service.Name=‘" + ServiceController.ServiceName + "‘");            ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");            InParam["DesktopInteract"] = true;            ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);            ServiceController.Start();

描述:在自己寫的一個系統服務程式,需要經常用到“允許與案頭進行互動”的設定,網上很多使用修改註冊表的形式實現,我測試過,修改註冊表後,選中的勾是選上了,

但不能彈出應用程式;據說重啟電腦後可以,但我不想重啟,實際應用也不允許重啟,故沒有測試重啟是否可行的情況。

 

例如:

當我需要運行服務程式的時候,彈出我的應用程式,則要在Windows服務“允許服務與案頭互動”中打勾,

當我不想彈出應用程式介面的時候,則去掉其中的勾選。

實現方式:

1.在服務程式安裝時編程實現,ProjectInstaller.cs


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
//using System.Linq;
using Microsoft.Win32; //對註冊表操作一定要引用這個命名空間


namespace MonitorService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();            
            //this.Context.Parameters["ServerCode"].ToString(); // 讀取安裝時輸入的伺服器編號           
        }

        private void ProjectInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            //設定允許服務與案頭互動
            SetServiceTable("MonitorService");            
        }
        /// <summary>
        /// 設定允許服務與案頭互動 ,修改了註冊表,要重啟系統才會生效
        /// </summary>
        /// <param name="ServiceName">服務程式名稱</param>
        private void SetServiceTable(string ServiceName)
        {
            RegistryKey rk = Registry.LocalMachine;
            string key = @"SYSTEM/CurrentControlSet/Services/" + ServiceName;
            RegistryKey sub = rk.OpenSubKey(key, true);
            int value = (int)sub.GetValue("Type");
            sub.SetValue("Type", value | 256);
        }
    }
}

 

2.註冊表修改

onstart的時候修改註冊表   
   [HKEY_LOCAL_MACHINE"SYSTEM"CurrentControlSet"Services"你的服務名]   
   "Type"=dword:00000010   
   key    value+256   
   比如現在00000010是16+256=272   
   16精製就是00000110
 

3.SC程式修改, 允許與案頭進行互動

 在dos命令提示字元下輸入:
sc config MonitorService type= interact type= own

 斷行符號即可。

可以用批處理的方式實現,把下面代碼儲存為 myservice.bat 即可:

 rem 佈建服務程式為允許與案頭進行互動方式
@echo "準備停止服務程式..."
sc stop MyService
@echo "設定允許與案頭進行互動方式允許"
sc config MyService type= interact type= own
@echo "正在重新啟動服務..."
sc start MyService
@echo "啟動服務成功!"

 

取消“允許與案頭進行互動”

DOS命令提示字元下運行下面語句即可:

 sc config MyService type= own

 

 

經測試:1,2 可以選中“允許與案頭進行互動”,但啟動服務的時候,不能彈出應用程式的介面。

           3 可以完美實現所有要求。

至此,我遇到的問題也完美的得到解決。

 

 

用VS2003部署,讓服務程式安裝完後立即啟動服務並且選中“允許服務與案頭互動”及添加服務描述的方法

 

<textarea cols="50" rows="15" name="code" class="c-sharp">-----------立即啟動-------------- private void serviceInstaller1_AfterInstall(object sender, System.Configuration.Install.InstallEventArgs e) { ServiceController myService = new ServiceController("XJOAPigeonholeServer"); myService.Start(); myService.Dispose(); } 添加描述:1.1沒有直接方法,2.0裡有直接的方法 ServiceInstaller.Description //----------------------------添加服務描述資訊 開始 ------------ public override void Install(IDictionary stateServer) { Microsoft.Win32.RegistryKey system, //HKEY_LOCAL_MACHINE/Services/CurrentControlSet currentControlSet, //.../Services services, //.../ &lt;Service Name&gt; service, //.../Parameters - this is where you can put service-specific configuration config; try { //Let the project installer do its job base.Install(stateServer); //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(this.serviceInstaller1.ServiceName, true); //Add your service‘s description as a REG_SZ value named "Description" service.SetValue("Description","XJOA系統自動歸檔服務(BeijiOffice)"); //(Optional) Add some custom information your service will use... //允許服務與案頭互動 service.SetValue("Type",0x00000110); config = service.CreateSubKey("Parameters"); } catch(Exception e) { Console.WriteLine("An exception was thrown during service installation:/n" + e.ToString()); } } public override void Uninstall(IDictionary stateServer) { 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(this.serviceInstaller1.ServiceName, 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(stateServer); } } //---------------------------- 結束 ---------------------------- </textarea>

 

 

四、這一服務程式運行時沒有圖形介面?

不錯,剛才直接運行mfc1.exe時我們看到了圖形介面,但在服務列表中用右鍵菜單中的“啟動”時卻看不到任何介面。這該怎麼辦?

我們還需要在使用CreateService函數時(Install()中),加上一個參數,這樣才能允許程式與案頭互動,也就是可以顯示介面。這個參數是SERVICE_INTERACTIVE_PROCESS。

填加後的CreateService:

  SC_HANDLE hService = ::CreateService(
     hSCM, m_szServiceName, m_szServiceName,
     SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
     SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
     szFilePath, NULL, NULL, _T("RPCSS"), NULL, NULL);

  再次編譯mfc1,卸載服務後,安裝服務。我們可以看到,通過服務列表啟動mfc1,原有的對話方塊出現了。

如需將服務設為自動啟動,則將 SERVICE_DEMAND_START 改為 SERVICE_AUTO_START。

http://blog.csdn.net/jiangxinyu/article/details/5397060

設定c#windows服務描述及允許服務與案頭互動的幾種方法(作者部落格還有一大堆C#建立服務的文章)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.