2009-09-14 3 views
0

폴더에서 파일 순서 번호를 찾는 방법. 내 이미지 파일은 오름차순으로 IMAGE 폴더에 저장됩니다. 예를 들어이미지 파일 순서 번호

,

IMGA IMGB imgC 는 오름차순으로 번호를받는 방법 imgC 3

IMGB는 2, 내가

IMGA는 1 알고 싶어

을 imgD 파일 이름 당.

+0

실제로 사실이 아닙니다. 일반적으로 폴더 및 파일 세트는 무작위로 저장됩니다. 그러나 폴더 내용을 읽을 때 OS는 미리 지정한 순서대로 데이터를 표시합니다. 파일에 오름차순 파일 이름을 지정하면됩니다. –

답변

0

당신은 모든 파일 이름을 얻기 위해 DirectoryInfo를 사용하여이 작업을 수행 한 후 작업을해야 파일 인덱스

public static int SortFileNames(string yourDirectory, string selectedFile) 
    { 
    DirectoryInfo directory = new DirectoryInfo(yourDirectory); 
    FileInfo[] files =directory.GetFiles(); 
    return GetFileIndex(files, selectedFile); 
    } 

    private static int GetFileIndex(FileInfo[] files, string selectedFile) 
    { 
    for(int i=0; i< files.length; i++) 
    { 
     if(files[i].Name== selectedFile) 
      return i; 
    } 
    return -1; 
    } 
+0

내 파일은 img *에서 시작할 수있을뿐만 아니라. 나는 달라질 것이다. 다른 경우, 폴더의 파일 이름 순서를 어떻게 미세하게 할 수 있습니까? – nithi

+0

나는 당신이 말하는 것을 이해하지 못합니다. – Graviton

+0

폴더에서 파일 순서 번호를 찾는 방법. 내 이미지 파일은 오름차순으로 IMAGE 폴더에 저장됩니다. 예를 들어 , IMGA IMGB imgC imgD 은 내가 IMGB 방법 파일 이름에 따라 오름차순으로 번호를 얻을 수 imgC 3 2, IMGA는 1 알고 싶어요.? – nithi

0

얻을 수 있습니다, 조금 더 간단

var imagesIndexes = Directory.GetFiles(directoryPath, "*.jpg") 
        .Select(fullPath => Path.GetFileName(fullPath) 
        .OrderBy(fileName => fileName) 
        .Select((fileName, index) => new {FileName = fileName, Index = index}) 
        .ToDictionary(item => item.FileName, item => item.Index); 


int indexOfFoo = imagesIndexes["Foo.jpg"]; 
int indexOfBar = imagesIndexes["Bar.jpg"]; 

을 또는을 :

var images = Directory.GetFiles(directoryPath, "*.jpg") 
      .Select(fullPath => Path.GetFileName(fullPath) 
      .OrderBy(fileName => fileName); 


int indexOfFoo = images.IndexOf("Foo.jpg"); 
int indexOfBar = images.IndexOf("Bar.jpg");