首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#不使用System.IO.FileInfo获取文件属性

C#不使用System.IO.FileInfo获取文件属性
EN

Stack Overflow用户
提问于 2013-01-04 16:01:11
回答 3查看 2.5K关注 0票数 1

是否可以在不使用System.IO.FileInfo的情况下从给定文件中提取“某些”属性?

我的意思是,我喜欢在只有几个文件需要处理的情况下使用FileInfo,但是,例如,如果我想从50万个文件中获取一个文件名的列表,我决定使用FileInfo.这需要-该死的-永远!

我怀疑FileInfo会将有关该文件的大量其他内容加载到内存中(对吗?)如果是这样的话,我觉得应该有一个更快的方法,比如说,获取名称或文件扩展名。

另一方面,我可以只使用Directory.GetFiles(myPath),但是这为每个文件提供了一个带有全路径的数组,而我只需要每个文件的名称!(我想我可以解析路径字符串,从中提取名称.这比使用FileInfo更快吗?)

如果我想更快地完成这个任务(获取文件的名称,或者获取文件扩展名),还有其他的选择吗?

非常感谢!!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-04 16:02:20

您正在寻找Path类中的方法。

具体来说,Path.GetFileName()Path.GetExtension()

票数 3
EN

Stack Overflow用户

发布于 2013-01-04 17:41:57

受你问题的启发,我比较了FileInfo和FileSystemObject。我测量了两个人穿越SSD驱动器所需的时间。

从本质上说, FileInfo比FileSystemObject.快三倍。

我重复了对我的系统的测量,以排除缓存效果:

代码语言:javascript
复制
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。

守则:

代码语言:javascript
复制
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)
            {
            }
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2013-01-04 16:03:13

如果您只感兴趣的话,可以使用Path.GetFilename获取文件名。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14160489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档