2012-04-20 12 views
-1

私はセレニウムのjarファイルを含むバッチスクリプトを持っています。私はC#からバッチスクリプトを実行したい。それが可能なのか分かりますか?実行中の(jarファイル)Cからバッチスクリプト#

example.bat

のjava -jar "セレンジャーへのパス"

は、私はC#からこのexample.batを実行します。

答えて

1

使用Process and ProcessStartInfo

command String内のPurあなたのjavaコマンド、またはバッチファイル名。

あなたが非同期的にそれを実行したい場合は、これを試してください
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 
    } 
} 
+0

しかし、jarファイルが実行されていない、コマンドプロンプトは偽CreateNoWindow =を入れ@saisindhu –

+0

を遮断し、私たちに何を教えています分かりますか。 – LaGrandMere

+0

@saisindhu出力を追加しました。コンソールに書き込む必要があります。何が起こるかを見るのに役立ちます。 – LaGrandMere

関連する問題