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

标签 c# file-permissions

我想在 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/

相关文章:

c# - 有没有一种简单的方法可以验证数据库模式是否正是我期望它使用 Entity Framework 的方式?

c# - 在 Visual Studio 2013 中查看受限的 WCF 调用

java - AccessController.checkPermission 的奇怪行为

windows - 无法在 cmd、Powershell 或 Vscode 中创建或更新文件。但我可以通过 Sublime、git bash 和文件资源管理器

Unix 权限 : How do groups work in Unix

c# - 自定义控件覆盖命令按钮

c# - C# Windows 窗体中的 VLC 视频播放器不播放 .flv 网络文件

c# - 复数瑞典语单词?

linux - 授予特定用户对文件的访问权限

ubuntu - 将权限设置为777,结果文件为775