做項目需要對Windows服務進行操作,從網上找了一些資料,總結如下:
(以下程序在程序中測試通過)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; using System.Configuration.Install; using System.Collections.Specialized; using System.ServiceProcess; namespace IAU.ServerInstall.BLL { public class ServiceControl { /// <summary> /// 註冊服務(註冊完就啓動,已經存在的服務直接啓動。) /// </summary> /// <param name="strServiceName">服務名稱</param> /// <param name="strServiceInstallPath">服務安裝程序完整路徑(.exe程序完整路徑)</param> public void Register(string strServiceName, string strServiceInstallPath) { IDictionary mySavedState = new Hashtable(); try { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(strServiceName); //服務已經存在則卸載 if (ServiceIsExisted(strServiceName)) { //StopService(strServiceName); UnInstallService(strServiceName, strServiceInstallPath); } service.Refresh(); //註冊服務 AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); mySavedState.Clear(); myAssemblyInstaller.Path = strServiceInstallPath; myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Install(mySavedState); myAssemblyInstaller.Commit(mySavedState); myAssemblyInstaller.Dispose(); service.Start(); } catch (Exception ex) { throw new Exception("註冊服務時出錯:" + ex.Message); } } /// <summary> /// 卸載服務 /// </summary> /// <param name="strServiceName">服務名稱</param> /// <param name="strServiceInstallPath">服務安裝程序完整路徑(.exe程序完整路徑)</param> public void UnInstallService(string strServiceName, string strServiceInstallPath) { try { if (ServiceIsExisted(strServiceName)) { //UnInstall Service AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller(); myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = strServiceInstallPath; myAssemblyInstaller.Uninstall(null); myAssemblyInstaller.Dispose(); } } catch (Exception ex) { throw new Exception("卸載服務時出錯:" + ex.Message); } } /// <summary> /// 判斷服務是否存在 /// </summary> /// <param name="serviceName">服務名</param> /// <returns></returns> public bool ServiceIsExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController s in services) { if (s.ServiceName == serviceName) { return true; } } return false; } /// <summary> /// 啓動服務(啓動存在的服務,30秒後啓動失敗報錯) /// </summary> /// <param name="serviceName">服務名</param> public void StartService(string serviceName) { if (ServiceIsExisted(serviceName)) { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) { service.Start(); for (int i = 0; i < 30; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) { break; } if (i == 29) { throw new Exception("服務" + serviceName + "啓動失敗!"); } } } } } /// <summary> /// 停止服務(停止存在的服務,30秒後停止失敗報錯) /// </summary> /// <param name="serviceName"></param> public void StopService(string serviceName) { if (ServiceIsExisted(serviceName)) { System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running) { service.Stop(); for (int i = 0; i < 30; i++) { service.Refresh(); System.Threading.Thread.Sleep(1000); if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) { break; } if (i == 29) { throw new Exception("服務" + serviceName + "停止失敗!"); } } } } } } }
項目裏面一直在用Time.timeScale來做遊戲的 1倍 2倍整體加速,今天我仔細看了一下Time.timeScale才發現之前我理解錯了一些東西。 Time.timeScale可以控制Update 和LateUpdate 的執
編程不止,bug不止。 程序員每天都會遇到各種各樣的bug,如下,發佈的項目發生了一下錯誤: 當應用程序不是以 UserInteractive 模式運行時顯示模式對話框或窗體是無效操作。請指定 ServiceNotification 或
最近在做C#執行Sql腳本的程序,環境VS2012+Sql Server 2008。本地運行沒有錯誤,但是發佈到服務器上就報如下錯誤 “/”應用程序中的服務器錯誤。 未能加載文件或程序集“Microsoft.SqlServer.Sql
1.爲了能讓InputField弄個滾動條真的是翻遍了百度,最後還是在自己的努力下完成了這一壯舉。 首先在場景中添加一個ScrollRect(滾動視圖),以下是我的設置,可以根據自己的喜好設置,建議將慣性關掉。 2.在滾動視圖的Cont
模態窗體(ShowDialog)打開後,後面的主窗體就不能做任何操作了。 本博客要實現非模態窗體(show)實現模態窗體(ShowDialog)的一些效果(主窗體關閉,子窗體也要關閉。子窗體只能打開一個。) 同時,保留非模態窗體的一些特性
發現這篇文章有很多知識都是我現在正需要的,轉過來先! http://www.cnblogs.com/kissknife/archive/2008/11/17/1335271.html 1、ADO.NET相關對象一句話介紹1)DataAda
有時需要知道用戶電腦上裝沒有裝IIS服務,以及IIS服務虛擬目錄的物理路徑。下面這個方法實現此功能,如果拋出異常則是沒有裝IIS服務,否則返回用戶電腦上的虛擬目錄物理路徑絕對地址。 /// <summary> /// 獲取虛擬目錄的物理路
一、認識Job System Unity C# Job System是用於寫多線程代碼的,使用多線程能夠帶來高性能,提高code generation的質量,減少移動設備的耗電量等; 二、什麼是多線程 在單線程計算機系統中,一次只能同時執
現在越來越多的開始使用Win10系統,並且使用了高清顯示屏。 但是在C#開發時卻出現了軟件字體模糊,感覺羽化了的效果。 後來發現可以在manifest配置文件中添加適配dpi來解決。 具體方法如下: 然後編輯manifest文件中將如
一、什麼是代碼注入 C#程序編譯生成中間代碼IL,爲了實現問題修復和一些通用功能擴展,通常所使用的就是代碼注入。 xlua的熱修復方案即採用了代碼注入的方式,沒有污染C#代碼,也不需要提前埋點,十分方便。 再比如這篇文章講述了使用代碼注入
USBProtocol u = new UDisk(); u._插入USB設備(); Console.ReadKey(); abstract class USBProtocol {
一文讀懂C#的 堆、棧、值類型、引用類型 版權聲明:本文
C#連接mysql數據庫主要有兩種方法 1、 用MySQL DriverCS連接MySQL數據庫 2、 通過ODBC連接MYSQL數據庫 具體分析: 1、 使用MYSQL DriverCS 。 首先下載和安裝Mysql Driv
以下是轉貼:作者:觀海看雲 描述線程與進程的區別? 線程是比進程更小的處理模塊。進程和線程都是由操作系統所體會的程序運行的基本單元,系統利用該基本單元實現系統對應用的併發性。進程和線程的區別在於: 簡而言之,一個程序至少有一個進程,一
一.C#編碼規則 1.C#編碼嚴格區分大小寫 2.C#代碼過濾空格、回車符和製表符 3.C#的每句代碼都以“;”結尾。一句代碼可以放在多行,一行可以放多句代碼,string變量的值字符串不可跨行寫,如需跨行用轉義字符“ \