Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改驱动器中文件的属性?_C#_Wpf - Fatal编程技术网

C# 如何更改驱动器中文件的属性?

C# 如何更改驱动器中文件的属性?,c#,wpf,C#,Wpf,我正在尝试更改驱动器而不是文件夹中所有文件和文件夹的属性。应用程序从用户处获取驱动器号,如C:\或D:。这是我的密码: private void Unhide_Click(object sender, RoutedEventArgs e) { string driveLetter = DriveLetter.Text + ":\\"; var drivePass = new DirectoryInfo(driveLetter);

我正在尝试更改驱动器而不是文件夹中所有文件和文件夹的属性。应用程序从用户处获取驱动器号,如C:\或D:。这是我的密码:

        private void Unhide_Click(object sender, RoutedEventArgs e)
    {
        string driveLetter = DriveLetter.Text + ":\\";
        var drivePass = new DirectoryInfo(driveLetter);
        if (drivePass != null && Directory.Exists(driveLetter))
        {
            drivePass.Attributes &= ~FileAttributes.ReadOnly; //This is where the error pops up!
            drivePass.Attributes &= ~FileAttributes.Archive;
            drivePass.Attributes &= ~FileAttributes.Hidden;
            drivePass.Attributes &= ~FileAttributes.System;
        }
        else
        {
            MessageBox.Show("Please Enter the Drive Letter", "Error");
        }
    }

当应用程序到达更改属性的点时,我收到一个错误,它显示无效的文件或目录属性值。有人能帮我吗?

不要设置.Attributes目录,而是存储中间值FileAttributes temp=drivePass.Attributes&~FileAttribtes.ReadOnly。temp是一个有效值吗?似乎您根本无法设置驱动器根文件夹的任何属性。请尝试使用drivePass.attributes=drivePass.attributes进行验证。希望您不相信在根文件夹上设置属性会神奇地设置该驱动器中任何文件或子文件夹的属性。@克莱门斯,请用代码演示您的注释好吗?因为我是初学者,这将使我更好地理解它。谢谢。你可以在网上搜索C递归设置的文件属性。