2012-12-24 7 views

답변

12

/C 스위치를 cmd.exe으로 전달하면 명령을 실행하려고 함을 나타낼 수 없습니다.

string command = "/C \"echo test > test.txt\""; 
System.Diagnostics.Process.Start("cmd.exe", command).WaitForExit(); 

을 그리고 당신은 쉘 창을 표시하지 않으려면 다음과 같은 사용할 수 있습니다 : 또한 명령이 큰 따옴표에 넣어 것을 알

string command = "/C \"echo test > test.txt\""; 
var psi = new ProcessStartInfo("cmd.exe") 
{ 
    Arguments = command, 
    UseShellExecute = false, 
    CreateNoWindow = true 
}; 

using (var process = Process.Start(psi)) 
{ 
    process.WaitForExit(); 
} 
+0

하지만 그는 말합니다 (http://stackoverflow.com/questions/14020664/c-sharp-run-command-not-doing-the-command/14020720#comment19362727_14020664) –

+1

@Soner Gönül, 그렇습니다. 내 답변에서와 같이'/ C' 스위치를 사용하십시오. 읽어 봤니? –

0

Process 클래스 중 하나를 생성하지 않습니다 파일. 이 경우 File 클래스를 사용해야합니다. 예;

string path = @"c:\temp\test.txt"; 
     if (!File.Exists(path)) 
     { 
      // Create a file to write to. 
      using (StreamWriter sw = File.CreateText(path)) 
      { 
       sw.WriteLine("Hello"); 
       sw.WriteLine("And"); 
       sw.WriteLine("Welcome"); 
      } 
     } 
+2

나는 그가 어떤 파일을 만들려고 생각하지 않는다. 그는 프로세스를 실행하고이 프로세스의 표준 출력을 파일로 리디렉션하려고합니다. –

+0

흠, 이해가되지 않을 수 있습니다 .. –

0

이 종류의 당신이 시작할 수 있어야합니다

//create your command 
string cmd = string.Format(@"/c echo Hello World > mydata.txt"); 
//prepare how you want to execute cmd.exe 
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe"); 
psi.Arguments = cmd;//<<pass in your command 
//this will make echo's and any outputs accessiblen on the output stream 
psi.RedirectStandardOutput = true; 
psi.UseShellExecute = false; 
psi.CreateNoWindow = true; 
Process p = Process.Start(psi); 
//read the output our command generated 
string result = p.StandardOutput.ReadToEnd();