2013-05-21 2 views
6

안녕 나는 C# 및 배치 파일에 대한 질문이 있습니다. 배치 명령을 실행하고 C#의 문자열에 출력을 저장하고 싶습니다. 하지만 난 단지 파일을 실행할 수 있지만 문자열에이 내용을 저장하고 텍스트 상자에 표시하지 않습니다. C#에서 일괄 처리 명령을 어떻게 직접 실행할 수 있습니까?

내 배치 파일 :

오프 @echo

"C : \ lmxendutil.exe"-licstatxml -host serv005 -port 6200> C : \ 임시 \의 HW_Lic_XML.xml 메모장 C : \ 임시 \ HW_Lic_XML.xml 여기

내 C# 코드 :

private void btnShowLicstate_Click(object sender, EventArgs e) 
{ 
    string command = "'C:\\lmxendutil.exe' -licstatxml -host lwserv005 -port 6200"; 

    txtOutput.Text = ExecuteCommand(command); 
} 

static string ExecuteCommand(string command) 
{ 
    int exitCode; 
    ProcessStartInfo processInfo; 
    Process process; 

    processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); 
    processInfo.CreateNoWindow = true; 
    processInfo.UseShellExecute = false; 
    // *** Redirect the output *** 
    processInfo.RedirectStandardError = true; 
    processInfo.RedirectStandardOutput = true; 

    process = Process.Start(processInfo); 
    process.WaitForExit(); 

    // *** Read the streams *** 
    string output = process.StandardOutput.ReadToEnd(); 
    string error = process.StandardError.ReadToEnd(); 

    exitCode = process.ExitCode; 

    process.Close(); 

    return output; 
} 

나는 출력을 원한다. 내가 이것을 할 수 있다면 나는 배치 파일이 없다고 원한다. 나는이 batvchcommand를 C#에서 직접하고 싶다. 내가 할 수 있을까?

+0

http://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp – Rahul

+0

내 업데이트를보세요 ... – Tarasov

답변

5

"CMD.exe"를 사용하여 명령 줄 응용 프로그램을 실행하거나 출력을 검색 할 필요없이 "lmxendutil.exe"를 직접 사용할 수 있습니다.

이 시도 :

processInfo = new ProcessStartInfo(); 
processInfo.FileName = "C:\\lmxendutil.exe"; 
processInfo.Arguments = "-licstatxml -host serv005 -port 6200"; 
//etc... 

가 "명령"을 사용하도록 수정 작업을 수행합니다.

이 정보가 도움이되기를 바랍니다.

2

배치 파일이 출력을 생성하는 것처럼 보이지 않습니다. 명령 줄에서 실행하면 출력이 표시됩니까? bat 파일 줄에 > 연산자를 리디렉션 했으므로 출력을 XML 파일로 보내는 것처럼 보입니다.

출력을 XML 파일에 저장 한 경우 프로세스가 종료되면 C#을 사용하여 출력을로드해야합니다.

+0

내 업데이트를보세요 ... 내 명령과 함께 작동하지 않습니다. 하지만 에코 테스트와 나는 테스트를 얻을 – Tarasov

관련 문제