感谢您的支持!我现在有工作代码来扫描所有文件夹,子文件夹和文件。只需解决一个问题:
我没有在初始根目录中获得文件,只有子文件夹。我还需要为这些文件调用FileInfo
。
如何解决此问题而又无需过多修改代码?
private void ScanFolder(String prefix, String path)
{
try
{
string user = System.IO.File.GetAccessControl(path).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
DirectoryInfo di = new DirectoryInfo(path);
foreach (var dir in new DirectoryInfo(path).GetDirectories("*", SearchOption.TopDirectoryOnly))
{
listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + dir.Name + " (" + dir.Name.Length.ToString() + ") "); });
foreach (FileInfo fileInfo in dir.GetFiles())
{
listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + fileInfo.Name + " (" + fileInfo.Name.Length.ToString() + ") " + user + " " + fileInfo.FullName + " (" + fileInfo.FullName.Length.ToString() + ")"); });
}
ScanFolder(prefix + "—", dir.FullName);
}
}
catch
{
if (!this.IsDisposed)
{
listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add("Access Denied to : " + path); });
}
}
}
**文件应在此处**
13-9-legacy_vista_win7_64_dd_ccc_whql(37)
Radeon-软件-肾上腺素18.3.3-最小设置-180319_web(56)
—宾(3)
-本地化(12)
——— cs(2)
--—— da_DK(5)
——— de(2)
——— el_GR(5)
——— es_ES(5)
答案 0 :(得分:2)
到目前为止,您仅在根目录中查找目录。
您还想通过文件枚举:
private void ScanFolder(String prefix, String path)
{
try
{
string user = System.IO.File.GetAccessControl(path).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
DirectoryInfo di = new DirectoryInfo(path);
// Enumerate through the files here
foreach (FileInfo fileInfo in di.GetFiles())
{
listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + fileInfo.Name + " (" + fileInfo.Name.Length.ToString() + ") " + user + " " + fileInfo.FullName + " (" + fileInfo.FullName.Length.ToString() + ")"); });
}
// ----
// You can also use the DirectoryInfo you created earlier here
foreach (var dir in new di.GetDirectories("*", SearchOption.TopDirectoryOnly))
{
listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + dir.Name + " (" + dir.Name.Length.ToString() + ") "); });
foreach (FileInfo fileInfo in dir.GetFiles())
{
listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + fileInfo.Name + " (" + fileInfo.Name.Length.ToString() + ") " + user + " " + fileInfo.FullName + " (" + fileInfo.FullName.Length.ToString() + ")"); });
}
ScanFolder(prefix + "—", dir.FullName);
}
}
catch
{
if (!this.IsDisposed)
{
listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add("Access Denied to : " + path); });
}
}
}