2011-02-09 7 views
1

私は、コマンドプロンプトを経由して勝利サービスをインストールおよびアンインストールしたい「C#」をインストールし、コマンドプロンプトからWindowsサービスをアンインストールし、コード、次の「C#」​​

はあなたがいない私を助けてください

string strInstallUtilPath ="C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\"; 
string strInstallService = " InstallUtil.exe \"D:\\TestUser\\ServiceForPatch\\TestService\\bin\\Debug\\TestService.exe\"";       
ProcessStartInfo PSI = new ProcessStartInfo("cmd.exe"); 
PSI.RedirectStandardInput = true; 
PSI.RedirectStandardOutput = true; 
PSI.RedirectStandardError = true; 
PSI.UseShellExecute = false; 
Process p = Process.Start(PSI); 
System.IO.StreamWriter SW = p.StandardInput; 
System.IO.StreamReader SR = p.StandardOutput; 
SW.WriteLine(@"cd\");   
SW.WriteLine(@"cd " + strInstallUtilPath); 
SW.WriteLine(strInstallService); 
p.WaitForExit(); 
SW.Close(); 
+0

あなたが取得しているどのようなエラー?詳細はどうぞ。 – Anuraj

答えて

3

が動作していませんコマンドプロンプトを起動する必要があります。 InstallUtilを起動し、適切なパラメータを渡す必要があります。

コードスニペットを変更し、オプションを指定してinstallutilを呼び出し、出力を文字列とコンソールウィンドウに書き込みます。

 string strInstallUtilPath = @"C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\installutil.exe"; 
     string strInstallService = @"D:\TestUser\ServiceForPatch\TestService\bin\Debug\TestService.exe"; 

     ProcessStartInfo processStartInfo = 
      new ProcessStartInfo(strInstallUtilPath, String.Format("/i {0}", strInstallService)); 


     processStartInfo.RedirectStandardOutput = true; 
     processStartInfo.RedirectStandardError = true; 
     processStartInfo.UseShellExecute = false; 

     Process process = new Process(); 
     process.StartInfo = processStartInfo; 
     process.Start(); 
     process.WaitForExit(); 

     String output = process.StandardOutput.ReadToEnd(); 
     Console.WriteLine(output); 
0

.NET FrameworkのビルトインのServiceInstallerは、私のために正常に動作しませんでしたので、ここで私は、Windowsサービスをアンインストールするためにやったことだ:

private void UninstallExistingService() 
    { 
     var process = new Process(); 
     var startInfo = new ProcessStartInfo(); 
     startInfo.RedirectStandardInput = true; 
     startInfo.UseShellExecute = false; 
     startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
     startInfo.FileName = "cmd.exe"; 

     process.StartInfo = startInfo; 
     process.Start(); 

     using (var sw = process.StandardInput) 
     { 
      if (sw.BaseStream.CanWrite) 
      { 
       sw.WriteLine("{0} {1}", "net stop", _serviceName); 
       sw.WriteLine("{0} {1}", "sc delete", _serviceName); 
      } 
     } 

     process.WaitForExit(); 
    } 
関連する問題