我已经编写了一些相当快速的代码,自从将其更改为“SearchOption.AllDirectories”后,甚至只需加载表单/应用程序就需要一整分钟。
我能做些什么来让这个应用程序在加载时运行得更快吗?任何帮助/信息将不胜感激。
谢谢。
private const string path = @"R:\Folder One\Folder Two\Folder Three";
public frmMain()
{
InitializeComponent();
ListDirectory(treeView1, path);
treeView1.SelectedNode = treeView1.Nodes[0];
treeView1.SelectedNode.Expand();
}
private void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}
private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
{
if (!directory.Name.Contains("_")
&& !directory.Name.Contains("Word Versions")
&& !directory.Name.Contains("Visio Flowcharts"))
{
var dirFileCount = directory.EnumerateFiles(
"*.pdf", SearchOption.AllDirectories).Count();
dirFileCount += directory.EnumerateFiles(
"*.xls", SearchOption.AllDirectories).Count();
dirFileCount += directory.EnumerateFiles(
"*.doc", SearchOption.AllDirectories).Count();
dirFileCount += directory.EnumerateFiles(
"*.xlsx", SearchOption.AllDirectories).Count();
dirFileCount += directory.EnumerateFiles(
"*.docx", SearchOption.AllDirectories).Count();
dirFileCount += directory.EnumerateFiles(
"~*", SearchOption.AllDirectories).Count();
if (dirFileCount != 0)
directoryNode.Nodes.Add(CreateDirectoryNode(directory));
}
}
foreach (var file in directoryInfo.GetFiles())
{
if (file.Extension.Equals(".pdf")
|| file.Extension.Equals(".xls")
|| file.Extension.Equals(".doc")
|| file.Extension.Equals(".xlsx")
|| file.Extension.Equals(".docx"))
{
if (!file.Name.Contains("~")
|| !file.Name.Contains("$"))
directoryNode.Nodes.Add(new TreeNode(file.Name));
}
}
return directoryNode;
}