简体   繁体   English

如何访问目录的任何文件或子目录?

[英]How to access any file or sub-directory of a directory?

I have a root directory with a path defined as 我有一个根目录,其路径定义为

string MyPath = @"D:/Documents/Reonance Tunneling";

I have a delete method that will delete any selected sub-directory written as follows: 我有一个delete方法,该方法将删除任何选定的子目录,如下所示:

private void BntDeleteFolder_Click(object sender, EventArgs e)
    {
        TreeNode newSelected = TreeView1.SelectedNode;
        string DirectoryName = newSelected.Text;
        DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;

        try
        {
            if (MessageBox.Show("Are you sure you want to delete Folder " + "'" + DirectoryName + "'" + " ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                foreach (FileInfo file in nodeDirInfo.GetFiles())
                {
                    File.Delete(MyPath + "//" + DirectoryName + "//" + item.Text);
                 }                  

                Directory.Delete(MyPath + "//" + DirectoryName + "//");
                newSelected.Remove();
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

However if I try to delete any selected sub-directory of the sub-directory I get the following error: "Could not find part of the path... " or "The directory is not empty." 但是,如果我尝试删除子目录中的任何选定子目录,则会出现以下错误:“找不到路径的一部分...”或“目录不为空”。 Can anybody tell me how to fix this problem? 谁能告诉我如何解决这个问题?

You are recreating plenty of ListViewItems that are never used (as Scott Chamberlain pointed out). 您正在重新创建大量从未使用过的ListViewItem(如Scott Chamberlain所指出的)。

You already have a FileInfo instance. 您已经有一个FileInfo实例。 Simply use: FileInfo.Delete Method () on file . 只需使用: file上的FileInfo.Delete方法()

and when you are done use nodeDirInfo.Delete() as well. 完成后,也请使用nodeDirInfo.Delete() You should probably also check for Subdirectories, not only files. 您可能还应该检查子目录,而不仅仅是文件。

If you insist on using the static Directory - class, use Directory.Delete(nodeDirInfo.Fullname, recursive: true); 如果您坚持使用静态Directory类,请使用Directory.Delete(nodeDirInfo.Fullname, recursive: true);

nodeDirInfo可以删除其所有文件和目录( https://msdn.microsoft.com/zh-cn/library/c66e2tts(v=vs.110).aspx

 nodeDirInfo.Delete(true);

If you look at the documentation for the DirectoryInfo.Delete Method , you'll notice that it takes a bool that defines if it should also delete the subdirectories and all files in the directory. 如果查看DirectoryInfo.Delete Method的文档,您会注意到它带有一个bool ,该bool定义了是否还应该删除子目录和目录中的所有文件。 This is probably the easiest way to get around the "The directory is not empty" exception. 这可能是解决“目录不为空”异常的最简单方法。

To avoid the "Directory not found" exception, you can just check for it first: 为避免出现“找不到目录”异常,您可以先检查一下:

var fullPath = Path.Combine(MyPath, newSelected.Tag);

if (Directory.Exists(fullPath))
{
    new DirectoryInfo(fullPath).Delete(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM