2017-09-18 10 views
-1

C#, 파일 및 폴더의 네트워크 드라이브에서 폴더의 내용을 최적화하는 작업을하고 있습니다. 파일의 경우 FileName, File Size 및 DateModified가 필요하며 폴더의 경우 Name 및 DateModified 만 필요합니다.쉘 (DIR)을 사용한 빠른 디렉터리 목록

다양한 솔루션에 대해 StackOverflow를 검색하고 Directory.GetFiles를 사용하여 해결 했으므로 파일을 병렬로 처리하지 않으므로 EnumerateFiles를 사용할 때 이점이 없습니다.

4000 및 5 개의 하위 폴더가 포함 된 WAN을 통한 테스트 사례를 통해 GetFiles는 30 초 이상 걸릴 수 있지만 Windows는 2 초 이내에 폴더를 유지할 수 있습니다.

너무 많은 Windows API 코드에 들어가기를 원하지 않기 때문에 DIR 명령을 셸링하고 표준 출력을 리디렉션하고 입력을 구문 분석하는 것이 좋은 중간 영역이라고 생각했습니다. 예쁘지는 않지만 괜찮을 것입니다. 나는 거의 내가 원하는 것을이 코드를 발견

Process process = new Process(); 
process.StartInfo.FileName = "ipconfig.exe";   
process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardOutput = true;   
process.Start(); 

// Synchronously read the standard output of the spawned process. 
StreamReader reader = process.StandardOutput; 
string output = reader.ReadToEnd(); 

이에서 ipconfig.exe을 위해 잘 작동을하지만, DIR 내가 그것을 호출 할 수있는 방법을하므로, 어떠한 아이디어, exe를하지? 나는이 같은 리디렉션 할 :

DIR을 "\ MyNasDrive \ MyFolder에"

최악의 경우 내가 .bat 파일에서이 작업을 마무리 할 수 ​​있지만, 상당히 끔찍한 느낌.

감사합니다.

== 내 자신의 솔루션을 찾을 수 있지만, 당신이 그것을 잘못 아무것도 볼 수 있다면 나에게

string DirPath = "\\\\MYServer\\MyShare\\"; 

Process process = new Process(); 
process.StartInfo.FileName = "C:\\Windows\\System32\\cmd.exe"; 
process.StartInfo.Arguments = "/C DIR /-C \"" + DirPath + "\""; 
process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardOutput = true; 
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
process.StartInfo.CreateNoWindow = true; 
process.Start(); 

// Synchronously read the standard output of the spawned process. Note we aren't reading the standard err, as reading both 
// Syncronously can cause deadlocks. https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.110).aspx 
//if we need to do this in the future then might be able to use https://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline(v=vs.110).aspx 
StreamReader reader = process.StandardOutput; 
string output = reader.ReadToEnd(); 

process.WaitForExit(); 
process.Close(); 
+0

'EnumerateFiles 사용시 이점이 없습니다.'GetFiles 대신'EnumerateFiles'를 사용하면 코드가 얼마나 오래 걸릴까요? – mjwills

+0

코드는 EnumerateFile을 사용하여 동일한 속도로 실행되었습니다. 왜냐하면 EnumerateFile 호출 바로 뒤에 모든 값을 읽는 루프가 있어야했기 때문입니다. 내 코드는 모든 값을 읽지 않아도 진행될 수 없으므로 속도면에서 이점이 없다고 생각하는 이유는 무엇입니까? 필자가 파일을로드하는 경우 필자는이를 병렬 처리 할 수 ​​있지만 필자는 파일 이름, 크기 및 DateModified를 읽는 것입니다. –

답변

0

DIR은

cmd.exe가있는 exe를하지 않습니다 == 알려 주시기 바랍니다 exe. 이 매개 변수

전화 cmd.exe :

  • /C
  • dir

참고 : true로 인 UseShellExecute를 설정할 수 있습니다.