作者leicheong (睡魔)
看板C_Sharp
标题Re: [问题] 算档案个数的函数
时间Wed Oct 17 23:03:25 2007
※ 引述《colorsea (OH Yes! Babe~)》之铭言:
: ※ 引述《leicheong (睡魔)》之铭言:
: : 先 new 一个 System.IO.DirectoryInfo, 再
: : DirectoryInfo.GetDirectories()/GetFiles().Length
: : 这些问题这算没在本机装MSDN Library, 到
: : http://msdn2.microsoft.com/en-us/library/default.aspx
: : 找也应该不难.
: 感谢大大的回应orz
: 我原来也是这样写的,但是只要一遇到资料夹是空的,没有档案就会发生IO Exception
: 所以才上来po文,问看有没有别的写法,
: 後来的做法就是把DirectoryInfo.GetFiles().Length这行
: 用try catch包起来(方法好像很笨^^"),就算没有档案也不会有错误讯息呢^^
啊... 漏看了这篇... :P
看remarks:
If there are no files in the DirectoryInfo, this method returns an empty array
嗯, 所以回传的不会是null...
我写了一段程式码:
private void FolderTest()
{
// 下面为新建的空目录
System.IO.DirectoryInfo di = new DirectoryInfo("C:\\temp\\test");
MessageBox.Show(Convert.ToString(di.GetFiles().Length));
}
可以成功显示是 "0" 呢, 这会不会是其他原因? (例如权限那些)
这IOException确实可疑...
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 219.73.77.181
※ 编辑: leicheong 来自: 219.73.77.181 (10/17 23:04)