如何删除C#中的只读文件的目录? [英] How do I delete a directory with read-only files in C#?

查看:307
本文介绍了如何删除C#中的只读文件的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除包含只读文件的目录。哪种方法更好:


  • 使用 DirectoryInfo.Delete()


  • ManagementObject.InvokeMethod(删除)


使用 DirectoryInfo.Delete(),我必须手动关闭只读属性为每个文件,但 ManagementObject.InvokeMethod(删除)似乎并不需要。是否有任何情况下,其中一个较为preferable其他?

样code(test.txt的只读)。

第一种方式:

  DirectoryInfo的DIR =新的DirectoryInfo(@C:\\用户\\大卫\\桌面\\);
dir.CreateSubdirectory(测试);DirectoryInfo的试验=新的DirectoryInfo(@C:\\用户\\大卫\\桌面\\测试\\);
File.Copy(@C:\\用户\\大卫\\桌面\\ test.txt的,@C:\\用户\\大卫\\桌面\\测试\\ test.txt的);
File.SetAttributes(@C:\\用户\\大卫\\桌面\\测试\\ test.txt的FileAttributes.Archive);
test.Delete(真);

方式二:

  DirectoryInfo的DIR =新的DirectoryInfo(@C:\\用户\\大卫\\桌面\\);
dir.CreateSubdirectory(测试);DirectoryInfo的试验=新的DirectoryInfo(@C:\\用户\\大卫\\桌面\\测试\\);
File.Copy(@C:\\用户\\大卫\\桌面\\ test.txt的,@C:\\用户\\大卫\\桌面\\测试\\ test.txt的);字符串的文件夹= @C:\\用户\\大卫\\桌面\\测试;
字符串dirObject =Win32_Directory.Name ='+文件夹+';
使用(的ManagementObject的ManagementObject =新的ManagementObject(dirObject))
{
    managementObject.Get();
    ManagementBaseObject outParams = managementObject.InvokeMethod(删除,空,
    空值);
    //返回值应该为0,否则失败
    如果(Convert.ToInt32(outParams.Properties [返回值。值)!= 0)
    {
    }
}


解决方案

我写我自己的方法至极设置属性使其正常的每个文件和目录递归,然后删除它们:

 私有静态无效DeleteFileSystemInfo(FileSystemInfo fileSystemInfo)
{
    VAR的DirectoryInfo = fileSystemInfo作为DirectoryInfo的;
    如果(DirectoryInfo的!= NULL)
    {
        的foreach(在directoryInfo.GetFileSystemInfos变种儿童信息())
        {
            DeleteFileSystemInfo(儿童信息);
        }
    }    fileSystemInfo.Attributes = FileAttributes.Normal;
    fileSystemInfo.Delete();
}

I need to delete a directory that contains read-only files. Which approach is better:

  • Using DirectoryInfo.Delete(), or,

  • ManagementObject.InvokeMethod("Delete")?

With DirectoryInfo.Delete(), I have to manually turn off the read-only attribute for each file, but ManagementObject.InvokeMethod("Delete") doesn't appear to need to. Is there any situation where one is more preferable to the other?

Sample code (test.txt is read only).

First way:

DirectoryInfo dir = new DirectoryInfo(@"C:\Users\David\Desktop\");
dir.CreateSubdirectory("Test");

DirectoryInfo test = new DirectoryInfo(@"C:\Users\David\Desktop\Test\");
File.Copy(@"C:\Users\David\Desktop\test.txt", @"C:\Users\David\Desktop\Test\test.txt");
File.SetAttributes(@"C:\Users\David\Desktop\Test\test.txt", FileAttributes.Archive);
test.Delete(true);

Second way:

DirectoryInfo dir = new DirectoryInfo(@"C:\Users\David\Desktop\");
dir.CreateSubdirectory("Test");

DirectoryInfo test = new DirectoryInfo(@"C:\Users\David\Desktop\Test\");
File.Copy(@"C:\Users\David\Desktop\test.txt", @"C:\Users\David\Desktop\Test\test.txt");

string folder = @"C:\Users\David\Desktop\Test";
string dirObject = "Win32_Directory.Name='" + folder + "'";
using (ManagementObject managementObject = new ManagementObject(dirObject))
{
    managementObject.Get();
    ManagementBaseObject outParams = managementObject.InvokeMethod("Delete", null,
    null);
    // ReturnValue should be 0, else failure
    if (Convert.ToInt32(outParams.Properties["ReturnValue"].Value) != 0)
    {
    }
}

解决方案

I wrote my own method wich sets attributes to normal for each file and directory recursively, and then deletes them:

private static void DeleteFileSystemInfo(FileSystemInfo fileSystemInfo)
{
    var directoryInfo = fileSystemInfo as DirectoryInfo;    
    if (directoryInfo != null)
    {
        foreach (var childInfo in directoryInfo.GetFileSystemInfos())
        {
            DeleteFileSystemInfo(childInfo);
        }
    }

    fileSystemInfo.Attributes = FileAttributes.Normal;
    fileSystemInfo.Delete();
}

这篇关于如何删除C#中的只读文件的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆