C# 3.0 图像文件订单号
如何从文件夹中查找文件顺序号。我的图像文件按升序存储在图像文件夹中 比如说, 伊姆加 imgB imgC imgD 我想知道 imgA是1,imgB是2,imgC是3C# 3.0 图像文件订单号,c#-3.0,C# 3.0,如何从文件夹中查找文件顺序号。我的图像文件按升序存储在图像文件夹中 比如说, 伊姆加 imgB imgC imgD 我想知道 imgA是1,imgB是2,imgC是3 如何根据文件名获取升序编号 您可以使用DirectoryInfo获取所有文件名,然后获取文件索引 public static int SortFileNames(string yourDirectory, string selectedFile) { DirectoryInfo directory = ne
如何根据文件名获取升序编号 您可以使用
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;
}
publicstatic int-SortFileNames(stringyourdirectory,stringselectedfile)
{
DirectoryInfo directory=新的DirectoryInfo(yourDirectory);
FileInfo[]files=目录.GetFiles();
返回GetFileIndex(文件,selectedFile);
}
私有静态int GetFileIndex(FileInfo[]文件,字符串selectedFile)
{
对于(int i=0;i
应该有效:
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");
我的文件不仅从img*开始。我会与众不同。如果是不同的意思,我如何确定文件夹中文件名的顺序号。我不太明白你说的。如何从文件夹中找到文件顺序号。我的图像文件按升序存储在图像文件夹中。例如,imgA-imgB-imgC-imgD我想知道imgA是1,imgB是2,imgC-3如何根据文件名获得升序数。?实际上,不是真的。一般来说,文件夹存储的文件集和顺序都是随机的。但当读取文件夹内容时,操作系统通常会按预定顺序向您显示数据。您所做的只是为文件提供升序文件名。