webform調用windows服務

來源:互聯網
上載者:User

標籤: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服務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.