-1
我有一個包含硒jar文件的批處理腳本。我想從C#執行批處理腳本。我可以知道它有可能嗎?從c執行(jar文件)批處理腳本#
example.bat
Java的罐子 「路徑硒罐子」
我要執行從C#這example.bat。
我有一個包含硒jar文件的批處理腳本。我想從C#執行批處理腳本。我可以知道它有可能嗎?從c執行(jar文件)批處理腳本#
example.bat
Java的罐子 「路徑硒罐子」
我要執行從C#這example.bat。
使用Process and ProcessStartInfo
陳建你的java命令,或者在command
字符串您的批處理文件名。
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.
Console.WriteLine(result);
如果你想異步運行它,試試這個:
/// <summary>
/// Execute the command Asynchronously.
/// </summary>
/// <param name="command">string command.</param>
public void ExecuteCommandAsync(string command)
{
try
{
//Asynchronously start the Thread to process the Execute command request.
Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
//Make the thread as background thread.
objThread.IsBackground = true;
//Set the Priority of the thread.
objThread.Priority = ThreadPriority.AboveNormal;
//Start the thread.
objThread.Start(command);
}
catch (ThreadStartException objException)
{
// Log the exception
}
catch (ThreadAbortException objException)
{
// Log the exception
}
catch (Exception objException)
{
// Log the exception
}
}
但罐子沒有運行,在命令提示符下被切斷 – 2012-04-20 08:21:55
@saisindhu把CreateNoWindow =假,並告訴我們什麼你看。 – LaGrandMere 2012-04-20 08:24:06
@saisindhu我添加了輸出,它應該寫在您的控制檯。這將有助於看到會發生什麼。 – LaGrandMere 2012-04-20 08:25:49