是否可以在不使用System.IO.FileInfo的情况下从给定文件中提取“某些”属性?
我的意思是,我喜欢在只有几个文件需要处理的情况下使用FileInfo,但是,例如,如果我想从50万个文件中获取一个文件名的列表,我决定使用FileInfo.这需要-该死的-永远!
我怀疑FileInfo会将有关该文件的大量其他内容加载到内存中(对吗?)如果是这样的话,我觉得应该有一个更快的方法,比如说,获取名称或文件扩展名。
另一方面,我可以只使用Directory.GetFiles(myPath),但是这为每个文件提供了一个带有全路径的数组,而我只需要每个文件的名称!(我想我可以解析路径字符串,从中提取名称.这比使用FileInfo更快吗?)
如果我想更快地完成这个任务(获取文件的名称,或者获取文件扩展名),还有其他的选择吗?
非常感谢!!
发布于 2013-01-04 16:02:20
您正在寻找Path类中的方法。
具体来说,Path.GetFileName()和Path.GetExtension()。
发布于 2013-01-04 17:41:57
受你问题的启发,我比较了FileInfo和FileSystemObject。我测量了两个人穿越SSD驱动器所需的时间。
从本质上说, FileInfo比FileSystemObject.快三倍。
我重复了对我的系统的测量,以排除缓存效果:
test FileInfo Files: 489557 Directories: 66023
FileInfo traversal needed 12,07s
test FileSystemObject. Files: 489557 Directories: 66023
FileInfo traversal needed 38,59s尝试使用Windows可能是值得的。但是由于编组,调用和参数传递必须支付性能费。
一个国产的C实用程序需要大约8s扫描SSD。
守则:
using System;
using System.Linq;
using Scripting;
using System.Diagnostics;
using System.IO;
namespace akTest
{
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
testFileInfo(@"c:\");
watch.Stop();
o("FileInfo traversal needed " + (watch.ElapsedMilliseconds/1000.0).ToString("#.##") + "s");
watch.Start();
testFSO(@"c:\");
watch.Stop();
o("FileInfo traversal needed " + (watch.ElapsedMilliseconds / 1000.0).ToString("#.##") + "s");
o("");
o("Ciao!");
}
static void o(string s)
{
Console.WriteLine(s);
}
static void testFileInfo(string dir)
{
DirectoryInfo di = new DirectoryInfo(dir);
long fileCount = 0;
long dirCount = 0;
long calls = 0;
o("Testing FileInfo");
WalkDirectoryTree(di, ref fileCount, ref dirCount, ref calls);
o("testFileInfo completed. Files: " + fileCount + " Directories: " + dirCount + " Calls: " + calls);
}
static void testFSO(string dir)
{
FileSystemObject fso = new FileSystemObject();
Folder folder = fso.GetFolder(dir);
long fileCount = 0;
long dirCount = 0;
long calls = 0;
o("Testing FSO");
WalkDirectoryTree(folder, ref fileCount, ref dirCount, ref calls);
o("testFSO completed. Files: " + fileCount + " Directories: " + dirCount + " Calls: " + calls);
}
static void WalkDirectoryTree(DirectoryInfo root, ref long fileCount, ref long dirCount, ref long calls)
{
FileInfo[] files = null;
DirectoryInfo[] subDirs = null;
if (++calls % 10000 == 0)
o("" + calls);
try
{
files = root.GetFiles("*.*");
if (files != null)
{
fileCount += files.Count();
subDirs = root.GetDirectories();
dirCount += subDirs.Count();
foreach (DirectoryInfo dirInfo in subDirs)
{
WalkDirectoryTree(dirInfo, ref fileCount, ref dirCount, ref calls);
}
}
}
catch (Exception)
{
}
}
static void WalkDirectoryTree(Folder root, ref long fileCount, ref long dirCount, ref long calls)
{
Files files = null;
Folders subDirs = null;
if (++calls % 10000 == 0)
o("" + calls);
try
{
files = root.Files;
if (files != null)
{
fileCount += files.Count;
subDirs = root.SubFolders;
dirCount += subDirs.Count;
foreach (Folder fd in subDirs)
{
WalkDirectoryTree(fd, ref fileCount, ref dirCount, ref calls);
}
}
}
catch (Exception)
{
}
}
}
}发布于 2013-01-04 16:03:13
如果您只感兴趣的话,可以使用Path.GetFilename获取文件名。
https://stackoverflow.com/questions/14160489
复制相似问题