积极答复者
没想到,我读文件想用DirectoryInfo 代替 Directory,问题越来越多..请问怎么解决?

问题
-
//Get File/Directory List. private void GetFilesList(string strDir, string strFilePattern, System.Collections.ArrayList arrDirs, System.Collections.ArrayList arrFiles, bool bIsRecursive) { //None directory. if(string.IsNullOrEmpty(strDir)) throw new ArgumentNullException("Directory"); string strPath = Path.GetFullPath(strDir); DirectoryInfo directory = new DirectoryInfo(strPath); //Directory. DirectoryInfo[] directorys = directory.GetDirectories(); //string[] directorys = Directory.GetDirectories(strDir); foreach(var dir in directorys) { arrDirs.Add(dir); } //File. FileInfo[] files = directory.GetFiles(strFilePattern); //string[] files = Directory.GetFiles(strDir, strFilePattern); foreach(var f in files) { arrFiles.Add(f); } //Recursive. if(bIsRecursive) { foreach(var dir in directorys) { GetFilesList(dir.Name, strFilePattern, arrDirs, arrFiles,true); } } }
//其实我觉得,在这里我用递归的方式去读取文件/文件夹,效率没有多大变化.但是一开始我想是否会重复利用同一个路径.所以我想用 DirectoryInfo,但是问题越来越多了...我随意指定的路径,一开始是没有全路径,所以我用 Path来解决但是用Path之后,怎么它得到的路径不是指定路径下的 Stuff.搞到当前我的"VS解决方案下的...debug路径了... 在这里,如果用 "Directory..就几步到位...
答案
-
这样做:
拖拽一个TreeView到WinForm上,在Form_Load中先遍历全部硬盘分区,显示到TreeView上。
点击某个分区节点,遍历下属的字文件夹(第一层),显示出来,如果再次点击某个文件夹,而该文件夹又包含子文件夹,同样显示出来。
点击文件夹时候,在合适的地方(比如ListView中)显示所有的文件。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 Mike FengModerator 2013年6月24日 14:38
全部回复
-
我的忠告是:
1)使用TreeView一开始加载所有的硬盘分区盘符(C,D等)。
2)当点击特定盘符,只遍历其对应的文件夹下属子文件夹,当点击子文件夹,才遍历下属的所有文件。避免一次性递归带来效率问题底下。
3)另外,如果一定考虑递归,建议采用异步或者是线程方式处理。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
不太明白你说的什么。
你用的path是什么?在哪定义的?赋得什么值?
Mike Feng
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
这样做:
拖拽一个TreeView到WinForm上,在Form_Load中先遍历全部硬盘分区,显示到TreeView上。
点击某个分区节点,遍历下属的字文件夹(第一层),显示出来,如果再次点击某个文件夹,而该文件夹又包含子文件夹,同样显示出来。
点击文件夹时候,在合适的地方(比如ListView中)显示所有的文件。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已标记为答案 Mike FengModerator 2013年6月24日 14:38
-
你是说,用这种方式去取代"递归"去读取...是吧...
对!不存在递归,你点击一个,只是遍历它下属的全部东西,不包含下属的下属。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已编辑 ThankfulHeartModerator 2013年6月15日 9:58