C# 在C中设置文件权限#

C# 在C中设置文件权限#,c#,file-permissions,C#,File Permissions,我想在C#中将文件的权限设置为“无法删除”,仅可读。但我不知道怎么做。你能帮我吗?看一看。网上有很多关于如何使用它的例子 摘自该MSDN页面: FileAttributes attributes = File.GetAttributes(path); if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { // Show the file.

我想在C#中将文件的权限设置为“无法删除”,仅可读。但我不知道怎么做。你能帮我吗?

看一看。网上有很多关于如何使用它的例子

摘自该MSDN页面:

FileAttributes attributes = File.GetAttributes(path);

        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
        {
            // Show the file.
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
            File.SetAttributes(path, attributes);
            Console.WriteLine("The {0} file is no longer hidden.", path);
        } 
        else 
        {
            // Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now hidden.", path);
        }

您忘记在RemoveAttribute方法中复制,该方法是:

    private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
    {
        return attributes & ~attributesToRemove;
    }
这是关于属性(见jb的答案)还是权限,即读/写访问权限等。?在后一种情况下,请参见

从MSDN:

// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);

// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType));

// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
有关更具体的示例,请参见


在最初的问题中,听起来您想禁止
文件系统权限。删除
权限。

代码也在这里,上面的链接(File.AccessControl)是德语版本。英文版是为那些回答“不”的人准备的。对于德国精神病患者来说,我认为问题是关于“权限”而不是“属性”。。。