建立windows定時,系統服務,卸載和安裝詳細步驟

來源:互聯網
上載者:User

1 建立 windows 服務 項目



2 添加自訂設定檔案


3 配置基本參數,

在程式中可以如下使用 這裡定義的常量 Timing_WX_L.Settings1.Default.span,

4 設定安裝參數  可參考 http://jingyan.baidu.com/article/fa4125acb71a8628ac709226.html

一、開啟Service1.cs視圖介面

在視圖內右鍵-->添加安裝程式

二、項目中添加了ProjectInstaller.cs檔案,該檔案中視圖自動會添加倆個組件

serviceProcessInstaller1

serviceInstaller1


三、選中serviceProcessInstaller1組件,查看屬性,設定account為LocalSystem

四、在視圖內右鍵-->添加安裝程式

五、項目中添加了ProjectInstaller.cs檔案,該檔案中視圖自動會添加倆個組件

serviceProcessInstaller1

serviceInstaller1

六、選中serviceProcessInstaller1組件,查看屬性,設定account為LocalSystem

七、選中serviceInstaller1組件,查看屬性

設定ServiceName的值, 該值表示在系統服務中的名稱

設定StartType, 如果為Manual則手動啟動,預設停止,如果為Automatic為自動啟動

設定Description,添加服務描述

八、重建項目


---------------------------------------------------------------以上是準備工作----------------------------------------------------------------------


5 在程式運行主程式裡,填寫如下程式

 partial class Wx_pay_L : ServiceBase    {        Timer _timer = new Timer();        string filePath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "logWX.txt";// "D:\\ws.txt";        static int i = 0;        static int span = 0;        public Wx_pay_L()        {            InitializeComponent();        }        protected override void OnStart(string[] args)        {                      //時間間隔             span = Convert.ToInt32(Timing_WX_L.Settings1.Default.span);            _timer.AutoReset = true;            _timer.Interval = 1000 * span;// 預設*1000是1秒            _timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);            try            {                TextWriter sw = new StreamWriter(filePath, true);                sw.WriteLine("------" + System.DateTime.Now.ToString());                sw.WriteLine("服務啟動。");                sw.Flush();                sw.Close();            }            catch            { }            _timer.Start();        }        private void OnTimedEvent(object source, ElapsedEventArgs e)        {            try            {                string uri = "http://localhost:802/api/pay/everyGetAllData";                WebClient wc = new WebClient();                string r = wc.DownloadString(uri);            }            catch (Exception ex)            {                try                {                    TextWriter sw = new StreamWriter(filePath, true);                    sw.WriteLine("------" + System.DateTime.Now.ToString());                    sw.WriteLine(ex.Message);                    sw.Flush();                    sw.Close();                }                catch                { }            }        }        protected override void OnStop()        {            try            {                TextWriter sw = new StreamWriter(filePath, true);                sw.WriteLine("------" + System.DateTime.Now.ToString());                sw.WriteLine("服務停止。");                sw.Flush();                sw.Close();            }            catch            { }        }        //private static string GetAppConfig(string strKey)   這裡是直接引用 webconfig        //{        //    foreach (string key in ConfigurationManager.AppSettings)        //    {        //        if (key == strKey)        //        {        //            return ConfigurationManager.AppSettings[strKey];        //        }        //    }        //    return null;        //}      }

6 產生之後的bin目錄裡就有exe檔案,之後運行如下步驟安裝和卸載其為系統服務

         一、  點擊 開始,運行中輸入cmd,擷取命令提示字元

           win7需要已管理員的身份啟動,否則無法安裝

        二、輸入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 斷行符號
切換目前的目錄,此處需要注意的是,在C:\Windows\Microsoft.NET\Framework目錄下有很多類似版本,具體去哪個目錄要看項目的運行環境,例 如果是.net framework2.0則需要輸入 cd C:\Windows\Microsoft.NET\Framework\v2.0.50727

       三、輸入 InstallUtil.exe E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe 斷行符號
說明:E:\TestApp\Winform\WinServiceTest\WinServiceTest\bin\Debug\WinServiceTest.exe表示項目產生的exe檔案位置

     四、開啟服務,就可以看到已經安裝的服務了;

      卸載很簡單,開啟cmd, 直接輸入 sc delete WinServiceTest便可。還有問題可以參考檔案下面的參考網址


相關文章

聯繫我們

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