2013-01-12 2 views
1

프로젝트에서 명령 프롬프트를 사용해야합니다. 모든 것은 괜찮습니다. 그러나 출력은 내가 원하는 것이 아닙니다. 내가 이렇게하면 :C# 명령 프롬프트 출력

 ProcessStartInfo info = new ProcessStartInfo("cmd","/c dir c:\\test"); 
     info.RedirectStandardOutput = true; 
     info.RedirectStandardInput = true; 
     info.CreateNoWindow = true; 
     info.UseShellExecute = false; 
     Process p = new Process(); 
     p.StartInfo = info; 
     p.Start(); 
     string iii = p.StandardOutput.ReadToEnd(); 
     textBox1.Text = iii; 

결과는 괜찮습니다. 정확히 내가 원하는대로. 하지만 좀 더 명령을 보내야합니다. 그래서 나는 이것을하고있다 :

 ProcessStartInfo info = new ProcessStartInfo("cmd"); 
     info.RedirectStandardOutput = true; 
     info.RedirectStandardInput = true; 
     info.CreateNoWindow = true; 
     info.UseShellExecute = false; 
     Process p = new Process(); 
     p.StartInfo = info; 
     p.Start(); 
     StreamWriter write = p.StandardInput; 
     write.WriteLine("dir c:\\test"); 
     write.Close(); 
     string iii = p.StandardOutput.ReadToEnd(); 
     textBox1.Text = iii; 

그러나 결과는 이전과 같지 않다. cmd와는 달리 경로와 모든 것을 제공합니다. 원하지 않습니다. 명령 프롬프트에서만 결과가 필요합니다. 누군가가 도울 수 있기를 바랍니다. 내 문제를 읽어 주셔서 감사합니다.

+0

글쎄, 나는 당신이 "dir c : \\ test"를 언급하고 있다고 가정하고있다. 그것은 설명하기 쉽다 : 당신 스스로 그것을 쓰고있다. 이전처럼/c를 통해 전달하십시오. – JerKimball

+0

답장을 보내 주셔서 감사합니다. 내가 말했듯이이 빈 출력을 가지고 있습니다 ... – user1965804

답변

0

당신이하고 싶은 것을위한 구현을 보여 주었고, "왜 내 구현이 내가 기대 한대로하지 못하는가"라는 관점에서 질문을 제기했다. 우리가 한걸음 물러서서 "내가 무엇이 필요한지 ..."(WINI)라고 말하면 우리는 당신에게 진정한 대답을 줄 수 있습니다. 예를 들어

:

"나는 'C : \ 테스트'의 디렉토리 목록 필요"로 나는 위니을 경우 나는 당신에게 당신이 원하는 원시 데이터를 제공하는 실제 간단한 작은 콘솔 응용 프로그램을 만들 말할 것을 . 나머지는 서식을 지정하고 우려를 분리하는 것입니다.

그런 다음 자신에게 가장 적합한 형식으로 원하는대로 제공 할 수있는 검색 응용 프로그램이 있습니다. 그런 다음 파서를 사용하여 출력을 구문 분석합니다.

public class Hyperthetical 
{ 
    public void MyDemoCall(string root) 
    { 
     Console.WriteLine("-- start --"); 
     GiveMeADirectoryListingAsIWantIt(root) 
     Console.WriteLine("-- end --"); 
    } 

    public void GiveMeADirectoryListingAsIWantIt(string directory) 
    { 
     Console.WriteLine("Folder '{0}':", directory); 

     foreach (var filePath in System.IO.Directory.GetFiles(directory)) 
     { 
      var fileInfo = new FileInfo(filePath); 
      Console.WriteLine("File: '{0}', {1}", fileInfo.Name, fileInfo.Length); 
     } 

     foreach (var subFolders in System.IO.Directory.GetDirectories(directory)) 
     { 
      GiveMeADirectoryListingAsIWantIt(subFolders); 
     } 
    } 
} 

아니 직접적인 대답하지만, 다만 어쩌면 진짜 답 :

그래서 당신의 작은 콘솔 응용 프로그램과 같은 일을 할 수 있습니다.

+0

다른 질문을하는 것이 아닙니다 ... 왜이 방법으로 파일 목록을 가져 옵니까? –