標籤:read .net 使用者和組 [] util blog tco cmd eve
準備工作:0.電腦->管理->本機使用者和組->組->Administrator雙擊->隸屬->添加Network service->確定1.啟動windows服務Windows Installer2.建立winform項目 WindowsFormsApplication13.添加windows服務 service14.添加代碼 protected override void OnStart(string[] args) { if (args != null && args.Length > 0) { if (args[0] == "1") { string path = $@"d:\kxbbbb{DateTime.Now.ToLongDateString()}.txt"; File.Create($"{path}"); } else if (args[0] == "2") { string path = $@"d:\kxqqq{DateTime.Now.ToLongDateString()}.txt"; File.Create($"{path}"); } } // TODO: 在此處添加代碼以啟動服務。 }5.Main函數啟動/// <summary> /// 應用程式的主進入點。 /// </summary> [STAThread] static void Main() { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); ServiceBase[] serviceRun; serviceRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(serviceRun); }6.service1.cs右鍵查看設計器,再右鍵添加安裝程式,預設添加兩個serviceInstaller1和serviceProcessInstaller17.serviceInstaller1右鍵屬性修改Description 為這是一個測試服務8.serviceProcessInstaller1右鍵屬性 Account修改為NetworkService9.管理員開啟cmdcd C:\Windows\Microsoft.NET\Framework\v4.0.3031910.安裝服務InstallUtil.exe WindowsFormsApplication1.exe11.webform調用 protected void Page_Load(object sender, EventArgs e) { ServiceController service = new ServiceController("Service1"); //if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) //{ // service.Start();//開啟服務 //} //停止服務 service.Stop();//這行報錯:無法開啟電腦“.”上的 Service1 服務。 service.WaitForStatus(ServiceControllerStatus.Stopped); //啟動服務 string[] args = { "2" }; service.Start(args); service.WaitForStatus(ServiceControllerStatus.Running); }
webform調用windows服務