我想在C#中设置文件的权限为“无法删除”,只能读取。但我不知道该怎么做。你可以帮我吗 ?在C#中设置文件权限#
5
A
回答
7
看看File.SetAttributes()。网上有很多关于如何使用它的例子。
从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);
}
2
你忘了在方法的removeAttribute复制,这就是:
private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}
0
这是关于属性(见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
权利。
相关问题
- 1. 如何在C++中打开文件时设置文件权限?
- 2. 在java中设置NTFS文件权限
- 3. 在java中设置文件权限5
- 4. 设置文件权限
- 5. 设置C++文件夹中所有文件的权限
- 6. 如何在Qt/C++中设置Windows文件权限
- 7. 在C#中设置管理权限
- 8. 在C#.NET中设置NTFS权限
- 9. 在Vista上设置文件夹权限
- 10. 在aosp上设置文件权限
- 11. 在设置文件夹权限
- 12. 在ZipArchive中设置权限
- 13. 在MySQL中,设置权限
- 14. 在setup.py文件中设置文件权限
- 15. 在magento中设置文件夹/文件的权限
- 16. 在android中设置文件读取权限文件
- 17. 如何在C编程设置文件夹权限#
- 18. 如何用C权限设置权限/时间/等创建一个文件?
- 19. 如何设置文件权限 - Android
- 20. 将文件权限设置为android
- 21. Powershell - 设置文件夹权限
- 22. 设置文件系统权限
- 23. 如何从Perl设置文件权限?
- 24. 为mysqldump输出设置文件权限
- 25. 使用Rake设置文件权限
- 26. 上传文件和设置权限
- 27. 安装后设置文件权限
- 28. 文件INSTALL无法设置权限libippicv
- 29. 跨域设置文件夹权限
- 30. 文件权限设置与WinSCP赋予