C#版Windows服務(wù)安裝卸載小工具
前言
在我們的工作中,經(jīng)常遇到Windows服務(wù)的安裝和卸載,在之前公司也普寫過一個WinForm程序選擇安裝路徑,這次再來個小巧靈活的控制臺程序,不用再選擇,只需放到需要安裝服務(wù)的目錄中運行就可以實現(xiàn)安裝或卸載。
開發(fā)思路
1、由于系統(tǒng)的權(quán)限限制,在運行程序時需要以管理員身份運行
2、因為需要實現(xiàn)安裝和卸載兩個功能,在程序運行時提示本次操作是安裝還是卸載 需要輸入 1 或 2
3、接下來程序會查找當(dāng)前目錄中的可執(zhí)行文件并過濾程序本身和有時我們復(fù)制進來的帶有vhost的文件,并列出列表讓操作者選擇(一般情況下只有一個)
4、根據(jù)用戶所選進行安裝或卸載操作
5、由于可能重復(fù)操作,需要遞歸調(diào)用一下
具體實現(xiàn)
首先們要操作服務(wù),需要用 System.ServiceProcess 來封裝實現(xiàn)類
using System;
using System.Collections;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
namespace AutoInstallUtil
{
public class SystemServices
{
/// <summary>
/// 打開系統(tǒng)服務(wù)
/// </summary>
/// <param name="serviceName">系統(tǒng)服務(wù)名稱</param>
/// <returns></returns>
public static bool SystemServiceOpen(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
if (control.Status != ServiceControllerStatus.Running)
{
control.Start();
}
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 關(guān)閉系統(tǒng)服務(wù)
/// </summary>
/// <param name="serviceName">系統(tǒng)服務(wù)名稱</param>
/// <returns></returns>
public static bool SystemServiceClose(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
if (control.Status == ServiceControllerStatus.Running)
{
control.Stop();
}
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 重啟系統(tǒng)服務(wù)
/// </summary>
/// <param name="serviceName">系統(tǒng)服務(wù)名稱</param>
/// <returns></returns>
public static bool SystemServiceReStart(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
if (control.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
control.Continue();
}
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 返回服務(wù)狀態(tài)
/// </summary>
/// <param name="serviceName">系統(tǒng)服務(wù)名稱</param>
/// <returns>1:服務(wù)未運行 2:服務(wù)正在啟動 3:服務(wù)正在停止 4:服務(wù)正在運行 5:服務(wù)即將繼續(xù) 6:服務(wù)即將暫停 7:服務(wù)已暫停 0:未知狀態(tài)</returns>
public static int GetSystemServiceStatus(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
return (int)control.Status;
}
}
catch
{
return 0;
}
}
/// <summary>
/// 返回服務(wù)狀態(tài)
/// </summary>
/// <param name="serviceName">系統(tǒng)服務(wù)名稱</param>
/// <returns>1:服務(wù)未運行 2:服務(wù)正在啟動 3:服務(wù)正在停止 4:服務(wù)正在運行 5:服務(wù)即將繼續(xù) 6:服務(wù)即將暫停 7:服務(wù)已暫停 0:未知狀態(tài)</returns>
public static string GetSystemServiceStatusString(string serviceName)
{
try
{
using (var control = new ServiceController(serviceName))
{
var status = string.Empty;
switch ((int)control.Status)
{
case 1:
status = "服務(wù)未運行";
break;
case 2:
status = "服務(wù)正在啟動";
break;
case 3:
status = "服務(wù)正在停止";
break;
case 4:
status = "服務(wù)正在運行";
break;
case 5:
status = "服務(wù)即將繼續(xù)";
break;
case 6:
status = "服務(wù)即將暫停";
break;
case 7:
status = "服務(wù)已暫停";
break;
case 0:
status = "未知狀態(tài)";
break;
}
return status;
}
}
catch
{
return "未知狀態(tài)";
}
}
/// <summary>
/// 安裝服務(wù)
/// </summary>
/// <param name="stateSaver"></param>
/// <param name="filepath"></param>
public static void InstallService(IDictionary stateSaver, string filepath)
{
try
{
var myAssemblyInstaller = new AssemblyInstaller
{
UseNewContext = true,
Path = filepath
};
myAssemblyInstaller.Install(stateSaver);
myAssemblyInstaller.Commit(stateSaver);
myAssemblyInstaller.Dispose();
}
catch (Exception ex)
{
throw new Exception("installServiceError/n" + ex.Message);
}
}
public static bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
return services.Any(s => s.ServiceName == serviceName);
}
/// <summary>
/// 卸載服務(wù)
/// </summary>
/// <param name="filepath">路徑和文件名</param>
public static void UnInstallService(string filepath)
{
try
{
//UnInstall Service
var myAssemblyInstaller = new AssemblyInstaller
{
UseNewContext = true,
Path = filepath
};
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
catch (Exception ex)
{
throw new Exception("unInstallServiceError/n" + ex.Message);
}
}
}
}
接下來我們封裝控制臺的操作方法為了實現(xiàn)循環(huán)監(jiān)聽這里用了遞歸
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace AutoInstallUtil
{
class Program
{
static void Main(string[] args)
{
try
{
ServerAction();
}
catch (Exception ex)
{
Console.WriteLine("發(fā)生錯誤:{0}", ex.Message);
}
Console.ReadKey();
}
/// <summary>
/// 操作
/// </summary>
private static void ServerAction()
{
Console.WriteLine("請輸入:1安裝 2卸載");
var condition = Console.ReadLine();
var currentPath = Environment.CurrentDirectory;
var currentFileNameVshost = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName).ToLower();
var currentFileName = currentFileNameVshost.Replace(".vshost.exe", ".exe");
var files =
Directory.GetFiles(currentPath)
.Select(o => Path.GetFileName(o).ToLower())
.ToList()
.Where(
o =>
o != currentFileNameVshost
&& o != currentFileName
&& o.ToLower().EndsWith(".exe")
&& o != "installutil.exe"
&& !o.ToLower().EndsWith(".vshost.exe"))
.ToList();
if (files.Count == 0)
{
Console.WriteLine("未找到可執(zhí)行文件,請確認(rèn)當(dāng)前目錄有需要安裝的服務(wù)程序");
}
else
{
Console.WriteLine("找到目錄有如下可執(zhí)行文件,請選擇需要安裝或卸載的文件序號:");
}
int i = 0;
foreach (var file in files)
{
Console.WriteLine("序號:{0} 文件名:{1}", i, file);
i++;
}
var serviceFileIndex = Console.ReadLine();
var servicePathName = currentPath + "\\" + files[Convert.ToInt32(serviceFileIndex)];
if (condition == "1")
{
SystemServices.InstallService(null, servicePathName);
}
else
{
SystemServices.UnInstallService(servicePathName);
}
Console.WriteLine("**********本次操作完畢**********");
ServerAction();
}
}
}
到此為止簡單的安裝程序就寫完了,為了醒目我選了個紅色的西紅柿來做為圖標(biāo),這樣顯示些
源碼和程序:安裝卸載Windows服務(wù)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:WPF自動隱藏的消息框的實例代碼
欄 目:C#教程
本文標(biāo)題:C#版Windows服務(wù)安裝卸載小工具
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6347.html
您可能感興趣的文章
- 01-10C#實現(xiàn)自定義windows系統(tǒng)日志的方法
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10C#修改IIS站點framework版本號的方法
- 01-10C#微信開發(fā)(服務(wù)器配置)
- 01-10C#實例代碼之抽獎升級版可以經(jīng)表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,抽獎設(shè)置
- 01-10C# 調(diào)用 JavaWebservice服務(wù)遇到的問題匯總
- 01-10Windows系統(tǒng)中使用C#讀取文本文件內(nèi)容的小示例
- 01-10Windows中使用C#為文件夾和文件編寫密碼鎖的示例分享
- 01-10C# WCF簡單入門圖文教程(VS2010版)


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個骰子


