我想在 C# 中将文件的权限设置为“无法删除”,只能读取。但我不知道该怎么做。你能帮帮我吗?
最佳答案
这是关于属性(参见 jb. 的回答)还是权限,即读/写访问等?在后一种情况下,请参阅 File.SetAccessControl .
来自 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);
参见 How to grant full permission to a file created by my application for ALL users?一个更具体的例子。
在最初的问题中,听起来您想禁止 FileSystemRights.Delete
权限。
关于c# - 在 C# 中设置文件权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7590446/