폴더에서 파일 순서 번호를 찾는 방법. 내 이미지 파일은 오름차순으로 IMAGE 폴더에 저장됩니다. 예를 들어이미지 파일 순서 번호
,
IMGA IMGB imgC 는 오름차순으로 번호를받는 방법 imgC 3
IMGB는 2, 내가
IMGA는 1 알고 싶어
을 imgD 파일 이름 당.폴더에서 파일 순서 번호를 찾는 방법. 내 이미지 파일은 오름차순으로 IMAGE 폴더에 저장됩니다. 예를 들어이미지 파일 순서 번호
,
IMGA IMGB imgC 는 오름차순으로 번호를받는 방법 imgC 3
IMGB는 2, 내가
IMGA는 1 알고 싶어
을 imgD 파일 이름 당.당신은 모든 파일 이름을 얻기 위해 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;
}
얻을 수 있습니다, 조금 더 간단
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");
실제로 사실이 아닙니다. 일반적으로 폴더 및 파일 세트는 무작위로 저장됩니다. 그러나 폴더 내용을 읽을 때 OS는 미리 지정한 순서대로 데이터를 표시합니다. 파일에 오름차순 파일 이름을 지정하면됩니다. –