2014-06-05 2 views
11

확장명이없는 파일의 경로 문자열 배열을 가져오고 싶습니다. 도움이된다면 확장자가없는 바이너리 파일입니다. 예를 들어 C# 확장자가없는 파일의 파일 경로 얻기

, 나는 (, 아니 .* 그래서 아무 .txt, 아니 .csv)

/test/

가 나는 확장자가없는 단지의 경로와 파일 이름을 원하는 폴더에서 파일 경로의 그룹을로드하고

/test/dontWant.txt

/test/dontWant.csv

/test/doWant

내가 할 경우 :

String[] paths = Directory.GetFiles(fDir, "*.*", SearchOption.AllDirectories); 

나는 물론 그 디렉토리에 모든 것을 얻을.

그때 시도하는 경우 :

String[] paths= Directory.GetFiles(fDir, "*", SearchOption.AllDirectories); 

나는 아직도 그 디렉토리에 모든 것을 얻을 것입니다.

확장자가없는 파일을 가져 오는 방법이 있습니까? 사용 "*."


작업을했고, 나는 시작하는 것을 시도하지 않았는지 모르겠어요.

먼저 EnumerateFiles을 사용해야합니다.

+2

으로이 와일드 카드를 사용할 수있는이 와일드 카드

String[] paths = Directory.GetFiles(fDir, "*.", SearchOption.AllDirectories); 

으로 시도 할 수 있습니다. ", SearchOption.AllDirectories); ' – Grundy

+0

@Grundy Nope. 그것은 점으로 끝나는 파일 이름입니다. 나는 그런 파일을 모른다. – tnw

+0

@ tnw, 너 시도해? – Grundy

답변

11

이 도움이 될 것입니다

var filesWithoutExtension = System.IO.Directory.GetFiles(@"D:\temp\").Where(filPath => String.IsNullOrEmpty(System.IO.Path.GetExtension(filPath))); 
foreach(string path in filesWithoutExtension) 
{ 
    Console.WriteLine(path); 
} 

그것은 확장 O /에만 지정된 디렉토리에 승 모든 파일을 반환합니다. 모든 하위 디렉토리를 포함 시키려면 다음을 사용하십시오 : System.IO.Directory.GetFiles(@"D:\temp\", "*", SearchOption.AllDirectories).

UPDATE
사람이 제안한 것처럼, 그것은 덜 램을 소모하기 때문에 Directory.EnumerateFiles를 사용하는 것이 좋습니다.

+0

이 코드는 상사와 유사합니다. 콘솔 앱에서 테스트했습니다. – Shiva

8

두 번째 패스 필터를 수행해야합니다.

//If you are using .NET 3.5 you can still use GetFiles, EnumerateFiles will just use less ram. 
String[] paths = Directory.EnumerateFiles(fDir, "*.*", SearchOption.AllDirectories) 
          .Where(file => Path.GetFileName(file) == Path.GetFileNameWithoutExtension(file)) 
          .ToArray(); 

그래서 어떤이가하는 것은 그 모두가 같은 문자열을 반환하는 경우 GetFileNameGetFileNameWithoutExtension에, 그 다음 배열의 결과를 포함하여 파일 경로를 전달합니다.

+1

'Directory.EnumerateFiles'에 +1하고 RAM을 적게 사용하십시오. –

2

aleksey.berezan의 답변 대신 .NET 4 이상에서 다음 작업을 수행 할 수 있습니다. EnumerateFiles은 파일이 디렉토리 트리에서 트래버스 될 때 파일을 반환합니다.

foreach(var file in Directory.EnumerateFiles(fDir, "*.*", SearchOption.AllDirectories).Where(s => string.IsNullOrEmpty(Path.GetExtension(s)))) 
{ 

} 
21

당신은 또한 당신이`문자열 [] 경로 =의 Directory.GetFiles (FDIR를, "* 시도 Directory.EnumerateFiles

Directory.EnumerateFiles(fDir, "*.", SearchOption.AllDirectories); 
+0

이것은 분명히 더 많은 upvotes받을 가치가있어. – Habib

관련 문제