C# 列出根目录中的所有文件

C# 列出根目录中的所有文件,c#,recursion,fileinfo,getfiles,C#,Recursion,Fileinfo,Getfiles,谢谢你的支持!我现在有了扫描所有文件夹、子文件夹和文件的工作代码。只有一个问题需要解决: 我不获取初始根目录中的文件,只获取子文件夹。我还需要调用FileInfo获取这些文件 如何在不过度修改代码的情况下解决此问题 private void ScanFolder(String prefix, String path) { try { string user = System.IO.File.GetAccessControl(path).GetOwner(typeo

谢谢你的支持!我现在有了扫描所有文件夹、子文件夹和文件的工作代码。只有一个问题需要解决:

我不获取初始根目录中的文件,只获取子文件夹。我还需要调用
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-Software-Adrenalin-18.3.3-MinimalSetup-180319_web(56)
-垃圾箱(3)
--本地化(12)
---cs(2)
---da_DK(5)
---de(2)
---el_GR(5)

---es_es(5)

到目前为止,您只在根目录中查找目录。
您还希望通过以下方式枚举文件:

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); });
        }
    }
}

到目前为止,您只在根目录中查找目录。
您还希望通过以下方式枚举文件:

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); });
        }
    }
}

列出文件和删除文件的方法有些相似,因此应该是有帮助的。根据GetDirectories返回满足指定条件的子目录的名称。列出文件和删除文件的方法有些相似,因此应该是有帮助的。根据GetDirectories返回满足指定条件的子目录。