我想尝试制作自己的文件资源管理器。我有一个算法来枚举所有驱动器中的所有目录。但它运行得太慢了。这是我的代码:
public ExplorerForm()
{
InitializeComponent();
this.SuspendLayout();//Without this, it will be far more slow again!
string[] a = System.IO.Directory.GetLogicalDrives();// a is used for drive array
for(int b = 0; b < a.Length; b++)//B is an enumerator. Ussually it is only one letter
{
//Defining the node
TreeNode c = new TreeNode();//c i place for TreeNode
c.Text = a[b].Substring(0,2);
c.Tag = a[b];
ApplyNodes(a[b], ref c);
if(c != null) tv.Nodes.Add(a)
}
this.ResumeLayout(false);
}
private void ApplyNodes(string a, ref TreeNode b)//a=directory, b applied TreeNode
{
try{
List<string> c = new List<string>(Directory.EnumerateDirectories(a);//c = directories
if (c.Count == 0 ) return;
for(int d = 0; d < c.Count; d++)//d = enumerator.
{
TreeNode e = new TreeNode();//e = TreeNode
var z = c[b].Split(Convert.ToChar("/"));
e.Text = z[z.Length-1]
e.Tag = c[b];
ApplyNodes(c[b], e)
if(e != null) b.Nodes.Add(e)
}
}catch (UnauthorizedAccessException){
}catch (IOException){ //For test, It is removed. and my E: is not ready
}
}
电视是我的控制。它运行得很慢。当我删除选择的行时会显示,抛出 IOException 需要 10 多秒的时间。帮助我如何改进枚举。使用线程和部分更新除外。如果以后不能修复,请告诉我原因。