2016-06-29 3 views
-1

의 System.IO.Directory.GetFiles 파일이 있습니다. 예 : TB2.8.5_asd_asd 및 TB2.9.5_asd_asd. 난 단지 이름이 TB2.6.5_로컬 파일 폴더에 특정 파일 이름

string[] dirs = System.IO.Directory.GetFiles(ConfigurationManager.AppSettings[@"LocalFolderPath"], "*" + ConfigurationManager.AppSettings["LocalFilesExtension"]).Where(s => s.StartsWith("TB2.6.5_")).ToArray(); 
+0

무엇합니까'ConfigurationManag er.AppSettings [ "LocalFilesExtension"]'contains? '.'로 시작합니까? –

+0

가 <키 = "LocalFolderPath"값을 추가 = 'C : \ 사용자 RNKP74 \ 바탕 화면 \의 testfolder2를 \'/> <키 = "LocalFilesExtension"값을 추가 = ''/> – Yeep

+0

는 경로 및 확장 – Yeep

답변

2

Directory.GetFiles() 방법은 당신이 입력 매개 변수의 하나로서 검색 패턴을 지정할 수 있습니다로 시작하는 파일을 얻는 방법에 대해 설명합니다. 당신은 그것을 활용하여 요구 사항을 완성 할 수 있습니다. 그래서 코드는 다음과 같이 될 것입니다 : AllDirectories

string PathToDirectory=Path.Combine(ConfigurationManager.AppSettings[@"LocalFolderPath"], ConfigurationManager.AppSettings[@"LocalFilesExtension"]; 
string searchPattern="TB2.6.5_*.*"; 
string[] dirs = System.IO.Directory.GetFiles(PathToDirectory,searchPattern,SearchOption.TopDirectoryOnly).ToArray(); 

변경 SearchOption 당신이 하위 디렉토리 검색을 확장 할 경우, 당신은 요구 사항에 따라의 searchPattern을 변경할 수 있습니다.

업데이트 :Illegal characters in path.

이것은 구성에 저장하는 값에 따라 달라집니다. 경로 구분 기호로 사용 \\이 있다면 당신은 사용할 수 있습니다 즉., AppSettings["LocalFilesExtension"] 후 다음 @

+0

경로에 잘못된 문자가 있습니다. – Yeep

+0

문자열 PathToDirectory = "ConfigurationManager.AppSettings [@ 'LocalFolderPath'], '*'+ ConfigurationManager.AppSettings [ 'LocalFilesExtension']"; string [] dirs = System.IO.Directory.GetFiles (PathToDirectory, "TB2.6.5 _ *. *", System.IO.SearchOption.AllDirectories) .ToArray(); – Yeep

+0

@ 예 :이 이유는 config에 저장된 데이터입니다. 업데이트 노트 –

2

을 사용하여 내가 그것을 \\\\\\ 변환하기 때문에 \ 전에 만 구성에서 하나의 \이 경우 @을 포함 할 필요가 없습니다 필터 매개 변수를 설정하는 것이

Directory.GetFiles(String, String)

"filename.exe" = filters for filename.exe 
"filename.*" = filters all files with the name filename 
"*filename*" = contains filename 
+0

string [] dirs = System.IO.Directory.GetFiles ("path", " .exe "); – Yeep

+0

이름을 어떻게 삽입해야합니까? pls advice thx – Yeep

관련 문제