/// <summary>
/// 安装或卸载服务
/// 版本: v2.0
/// 作者: wenddy112
/// </summary>
/// <param name="bInstall">true:安装; false:卸载</param>
///
/// <returns></returns>
///
public static void InstallServer(bool bInstall)
{
try
{
string sFileName = Environment.SystemDirectory;
sFileName = sFileName.Substring(0,sFileName.LastIndexOf("\\")) + "\\Microsoft.NET\\Framework\\v1.1.4322\\installutil.exe";
//通过命令行来获取应用程序的名称
string [] commendline = Environment.GetCommandLineArgs();
string serviceFileName =
System.IO.Path.GetFileName(commendline[0]);
if (!bInstall)serviceFileName = "-u " + serviceFileName;
//如果应约程序名称没有扩展名就加上exe的扩展名
if (serviceFileName.Length < 4 ||
serviceFileName.Substring(serviceFileName.Length-4,4).ToLower() != ".exe")
{
serviceFileName += ".exe";
}
FileInfo TheFile = new FileInfo(sFileName);
Process myProcess = new Process();
if (TheFile.Exists)
{
myProcess.StartInfo = new ProcessStartInfo(TheFile.FullName,serviceFileName);
myProcess.StartInfo.CreateNoWindow = true;
//关闭新进程的shell
myProcess.StartInfo.UseShellExecute = false;
//需要重定向输入流
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.Start();
//获取进程输出信息并输出
StreamReader sr = myProcess.StandardOutput;
Console.Write(sr.ReadToEnd());
}
else
{
Console.WriteLine("没有发现.NET服务安装文件installutil.exe");
}
}
catch(Exception ex)
{
Console.WriteLine("调用installutil.exe发生意外!\n错误信息:"+ex.Message);
}
Console.ReadLine();
}