我必须检查磁盘上的目录是否为空.这意味着,它不包含任何文件夹/文件.我知道,有一个简单的方法.我们得到FileSystemInfo的数组并检查元素的数量是否等于零.像这样的东西:
public static bool CheckFolderEmpty(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
var folder = new DirectoryInfo(path);
if (folder.Exists)
{
return folder.GetFileSystemInfos().Length == 0;
}
throw new DirectoryNotFoundException();
}
Run Code Online (Sandbox Code Playgroud)
这种方法似乎没问题.但!!从表现的角度来看,这是非常非常糟糕的.GetFileSystemInfos()是一种非常难的方法.实际上,它枚举文件夹的所有文件系统对象,获取它们的所有属性,创建对象,填充类型化数组等.所有这些只是为了简单地检查长度.那是愚蠢的,不是吗?
我只是描述了这样的代码并确定,这种方法的〜250次调用在~500ms内执行.这是非常缓慢的,我相信,它可以更快地完成.
有什么建议?
我的目录包含*.wav格式的近14,000,000个音频样本.
所有普通存储,没有子目录.
我想循环遍历文件,但是当我DirectoryInfo.GetFiles()在该文件夹上使用时,整个应用程序冻结了几分钟!
这可以换一种方式吗?也许阅读1000,处理它们,然后采取下一个1000,依此类推?
我有一个包含几千个文件夹的基目录.在这些文件夹中,可以有1到20个子文件夹,其中包含1到10个文件.我想删除所有超过60天的文件.我使用下面的代码来获取我必须删除的文件列表:
DirectoryInfo dirInfo = new DirectoryInfo(myBaseDirectory);
FileInfo[] oldFiles =
dirInfo.GetFiles("*.*", SearchOption.AllDirectories)
.Where(t=>t.CreationTime < DateTime.Now.AddDays(-60)).ToArray();
Run Code Online (Sandbox Code Playgroud)
但我让它运行了大约30分钟,但仍然没有完成.我很好奇是否有人能够看到我可以提高上述线路的性能,或者如果有不同的方式我应该完全接近这个以获得更好的性能?建议?
使用以下方法进行转换时,我阅读的大多数文件都是合适的时间:
// works great most of the time
private static DateTime convertToDateTime(System.Runtime.InteropServices.ComTypes.FILETIME time)
{
long highBits = time.dwHighDateTime;
highBits = highBits << 32;
return DateTime.FromFileTimeUtc(highBits + time.dwLowDateTime);
}
Run Code Online (Sandbox Code Playgroud)
这里我在visual studio中有一个例子来说明这个方法有时不起作用,例如我将在我的计算机和调试中显示实际文件.所以恰好在我的调试中的文件是:
"A:\ Users\Tono\Documents\Visual Studio 2010\Projects\WpfApplication4\WpfApplication4\obj\x86\Debug\App.g.cs"

这里是我试图转换为DateTime的FILETIME"我需要LastWriteTime"

在这里你可以看到dwHighDateTime = 30136437以及该文件中的dwLowDateTime = -2138979250.
当我运行我的方法加上其他技术时,我得到以下日期:

所以到目前为止,一切似乎都很有效.但是为什么当我在Windows中浏览并查找特定文件时,我会得到一个不同的日期!这是我在查看文件属性时得到的日期:

为什么日期不匹配?我究竟做错了什么?
我正在开发文件同步服务,以便在不同计算机上的两个文件夹之间同步文件.我需要找到一种非常快速的方法来枚举目录并从中提取以下信息:
到目前为止,我已经想出了这个:
static void Main(string[] args)
{
List<Tuple<string, DateTime>> files = new List<Tuple<string, DateTime>>();
List<Tuple<string, DateTime>> directories = new List<Tuple<string, DateTime>>();
Stopwatch watch = new Stopwatch();
while (true)
{
watch.Start();
while (!CheckFolderRecursiveSingleThreaded("C:\\", out files, out directories))
{
// You can assume for all intents and purposes that drive C does exist and that you have access to it, which will cause this sleep to not get called.
Thread.Sleep(1000);
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
watch.Reset();
// Do something with the information. …Run Code Online (Sandbox Code Playgroud) 我之前问过快速获取特定路径中的所有文件和目录的问题,以便尽可能快地找到文件.我正在使用该解决方案,以便找到与正则表达式匹配的文件名.
我希望显示一个进度条,因为有一些非常大而慢的硬盘驱动器,它仍然需要大约1分钟才能执行.我在另一个链接上发布的解决方案无法让我知道为了让我显示进度条,还有多少文件丢失.
我正在考虑做的一个解决方案是尝试获取我正在计划遍历的目录的大小.例如,当我右键单击该文件夹时,C:\Users我可以估计该目录的大小.如果我能够知道大小,那么我将能够通过添加我找到的每个文件的大小来显示进度.换句话说,progress =(文件大小的当前总和)/目录大小
由于某种原因,我无法有效地获得该目录的大小.
有关堆栈溢出的一些问题使用以下方法:

但请注意,我得到一个例外,无法枚举文件.我很高兴在我的驱动器上尝试这种方法.
在那张照片上,我试图计算文件数量以显示进度.我可能无法使用该方法有效地获取文件数量.当人们问起how to get the number of files on a directory并且人们问起时,我只是尝试堆栈溢出的一些答案how the get the size f a directory.
我需要我的程序来计算磁盘驱动器上的文件数量.
最快的方法是什么?
Directory.GetFiles()不是替代品,因为它非常慢.
我递归地使用这个 apreach快速查找目录中的所有文件.
无论如何,我将信息存储在结构中的每个文件中:
struct Info
{
public bool IsDirectory;
public string Path;
public FILETIME ModifiedDate;
}
Run Code Online (Sandbox Code Playgroud)
所以现在我正在尝试决定天气将辅助方法放在该结构或其他地方以提高效率.
辅助方法是:
struct Info
{
public bool IsDirectory;
public string Path;
public FILETIME ModifiedDate;
// Helper methods:
public string GetFileName(){ /* implementation */ }
public string GetFileSize(){ /* implementation */ }
public string GetFileAtributes() { /* implementation */ }
// etc many more helper methods
}
Run Code Online (Sandbox Code Playgroud)
我在内存中保存了数千个文件,我不知道在Info中使用这些方法是否会影响性能.换句话说,最好删除这些方法并使它们成为扩展方法:
public static class ExtensionHelperMethods
{
static public string GetFileName(this Info info){ /* implementation */ }
static …Run Code Online (Sandbox Code Playgroud) 我想加快遍历树的过程.以下是节点的示例:
class Node
{
public List<Node> Children { get; set; }
public int SompeProperty { get; set; }
public String SomeOtherProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我遍历尝试的方式如下:
static void TraverseTree(Node ParentNode)
{
if (ParentNode.Children == null)
return;
foreach (var child in ParentNode.Children)
{
TraverseTree(child);
}
}
Run Code Online (Sandbox Code Playgroud)
该ParentNode.Children方法大约需要1毫秒,因为Node表示文件或目录.我只是用这个节点的例子来说明我的观点.
因此,如果您考虑一下,如果第一个节点有4个子节点,并且每个子节点都有10000000个后代,那么如果我们在separeate线程中利用并行编程来遍历这4个子节点中的每一个,我们可以提高此遍历的速度.如果那就是情景那么我会采取这种方法.但如果我事先不知道树的结构怎么能这样做呢?
我一直在考虑:
1)开始遍历树,将具有子节点的前10个节点放在堆栈上,然后在单独的线程上开始遍历每个节点.
2)做类似的事情:
static void TraverseTree(Node ParentNode)
{
if (ParentNode.Children == null)
return;
foreach (var child in ParentNode.Children)
{
ThreadPool.QueueUserWorkItem(new WaitCallback((x) =>
{
TraverseTree(child);
}), null);
}
}
Run Code Online (Sandbox Code Playgroud)
这通常会给我带来奇怪的结果,但速度要快得多.
使用C#在磁盘上查找所有exe文件的最有效方法是什么?
它将在程序中的后台线程中完成,因此磁盘使用量应尽可能小.
c# ×10
.net ×4
performance ×3
directory ×2
io ×2
boost ×1
datetime ×1
file ×1
filesystems ×1
filetime ×1
getfiles ×1
traversal ×1
visual-c++ ×1
windows ×1
windows-7 ×1