2011-08-09 5 views
0

확장명이 .dcm 인 파일을 검색하려면 파일 이름이나 파일 내용을 입력하십시오. 디렉토리에서 검색 할 수 있지만 드라이브에서 검색을 시도하면 디렉토리가 누락되었거나 참조를 어셈블하는 오류가 발생합니다.일반적으로 드라이브에서 .dcm 확장명의 파일 검색 C#

string startFolder = @"C:\"; 
// Take a snapshot of the file system. 
System.IO.DriveInfo dir = new System.IO.DriveInfo(startFolder); 

// This method assumes that the application has discovery permissions 
// for all folders under the specified path. 
IEnumerable<System.IO.FileInfo> fileList = dir.GetFiles("*.*", System.IO. 
                   SearchOption. 
                   AllDirectories); 

제발 도와주세요. 감사! 폴더에 작동처럼

+0

그냥 그것을 지적하고 싶어요 당신은' ".dcm *"'으로 당신의'searchPattern'을 변경할 수 있습니다 ''*. * ''대신''*. * "' –

답변

3

봅니다 드라이브에 검색 할 :

string startFolder = @"c:\"; 
    DirectoryInfo directoryInfo = new DirectoryInfo(startFolder); 
    IEnumerable<System.IO.FileInfo> fileList = directoryInfo.GetFiles("*.*", System.IO.SearchOption.AllDirectories); 
+0

안녕하세요.이 오류가 발생합니다. 'C : \ System Volume Information'경로에 대한 액세스가 거부되었습니다. – puzzled

+0

확인. 이 경우 직접 재귀 알고리즘을 구현하고 sequrity 예외를 catch해야합니다. 내가 당신을 위해 코드 스 니펫 (snippet)을 발견했는지 보도록하겠습니다. –

+0

나는 거기에 최근에 그것을 구현했다 http://sourcecodecloud.codeplex.com/. –

0

당신이 중 하나를 찾기 위해 그 사용 foreach 루프 후 .dcm 확장

string startFolder = @"c:\"; 
DirectoryInfo directoryInfo = new DirectoryInfo(startFolder); 
IEnumerable<System.IO.FileInfo> fileList = directoryInfo.GetFiles("*.dcm", System.IO.SearchOption.AllDirectories); 

*로 immediatelly 모든 파일을 필터링 할 수 있습니다 원하는 이름을 가진 파일을 열거 나 읽기 위해 파일을 열고 파일 안의 값을 검색하십시오.

* .dcm 텍스트 기반 파일 또는 바이너리 파일입니까? 텍스트 기반 파일 인 경우 regulare 표현식을 사용하여 검색 문자열의 존재 여부를 확인할 수 있습니다.

편집 : 다음은 작업을 수행하는 재귀 함수의 예입니다.

이 기능은 try-catch 블록을 사용하여 발생할 수있는 예외를 가로 챌 수 있습니다. 예를 들어 파일을 찾을 수 없거나 액세스가 거부되었습니다. 희망이 도움이! 예외 피하기 위해

+0

안녕하세요, 내가 할 때 같은 오류가 발생합니다 : 'C : \ System 볼륨 정보'경로에 대한 액세스가 거부되었습니다. .dcm은 IMA 파일입니다. – puzzled

+0

제 EDIT보세요. 적합하다고 생각되는대로 수정하십시오. 재귀 함수 내에서 * .txt를 * .dcm으로 변경하십시오. 원하는 경우 최적화하십시오. 즐겨. –

0

사용이 재귀 함수 : 당신이 * .dcm` 파일`를 검색하기 때문에

public static IEnumerable<string> GetFiles(string path, string pattern) 
{ 
    IEnumerable<string> result; 
    try 
    { 
     result = Directory.GetFiles(path, pattern); 
    } 
    catch (UnauthorizedAccessException) 
    { 
     result = new string[0]; 
    } 

    IEnumerable<string> subDirectories; 
    try 
    { 
     subDirectories = Directory.EnumerateDirectories(path); 
    } 
    catch (UnauthorizedAccessException) 
    { 
     subDirectories = new string[0]; 
    } 

    foreach (string subDirectory in subDirectories) 
    { 
     IEnumerable<string> subFiles = GetFiles(subDirectory, pattern); 
     result = result.Concat(subFiles); //This is LINQ concatenation 
    } 
    return result; 
} 

static void Main(string[] args) 
{ 
    string startFolder = @"c:\"; 
    foreach (string fileName in GetFiles(startFolder, "*.chm")) 
    { 
     Console.WriteLine(fileName); 
    } 
+0

안녕하세요, 내가이 일을 할 때 System.IO.Directory 'EnumerateDirectories'.When에 대한 정의가 포함되어 있지 않다는 오류 메시지가 나타납니다. NET 4 버전이 필요하고 .NET 3.0 있습니다. 나는 무엇을 다음에 할 수 있 었는가? 감사. – puzzled

+0

대신 'GetDirectories()'를 사용해보십시오. –

관련 문제