在使用windows服務時,安裝能有些不太方便,本文就安裝時就使用的技巧提出一點方式,以供大家參考:
windows服務的優點還是加以說明 的1、能夠自動運行(需要預先設置運行模式); 2、不要求用戶交互,(特別適合在服務器上進行運行);3、在後臺運行,如果程序運行時,特別超耗費時間,如備分數據,刪除大量數據,多線程捕獲數據等等),都可能使用。
但是在安裝時,經常會找不到這個關鍵的文件installutil.exe,因爲此文件是用來支持安裝過程的。
詳細介紹見http://www.cnblogs.com/YanPSun/archive/2010/05/22/1741381.html,
這是就給出一種調用的方式方法。
try
{
System.Uri uri = new Uri(typeof(string).Assembly.CodeBase);
string RuntimePath = System.IO.Path.GetDirectoryName(uri.LocalPath);
string strInstallUtilPath = System.IO.Path.Combine(RuntimePath, "InstallUtil.exe");
foreach (string arg in System.Environment.GetCommandLineArgs())
{
Console.WriteLine(arg);
if (arg == "/install")
{
System.Diagnostics.Process.Start(strInstallUtilPath, "\"" + System.Windows.Forms.Application.ExecutablePath + "\"");
return;
}
else if (arg == "/uninstall")
{
System.Diagnostics.Process.Start(strInstallUtilPath, "/u \"" + System.Windows.Forms.Application.ExecutablePath + "\"");
return;
}
else if (arg == "/client")
{
// 啓動客戶端
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (Form1 frm = new Form1())
{
Application.Run(frm);
}
return;
}
}
}
catch (Exception ext)
{
Console.WriteLine(ext.ToString());
return;
}
還要進一步說明的,服務開始執行的語句放到
protected override void OnStart(string[] args)
{
’執行動作的開始
}
停止服務的語句放到
#region 停止服務線程.
protected override void OnStop()
{
'停止工作
base.OnStop();
}
#endregion
以上工作是受服務啓動,和停止來執行的。