以编程方式向 C# 中的文件添加安全权限
Posted
技术标签:
【中文标题】以编程方式向 C# 中的文件添加安全权限【英文标题】:Programmatically adding security permissions to files in C# 【发布时间】:2010-11-05 09:55:20 【问题描述】:在一个 asp.net 应用程序中,我有一个任务是将一些 xml 文件传输到我计算机上的本地文件夹中。然后我想读取这些文件,但是当它们被复制到我的本地文件夹时,它们没有设置网络服务用户帐户。所以,我的问题是,在 .Net C# 中,您如何以编程方式将具有完全控制权的“网络服务”帐户添加到我的 xml 文件中。
【问题讨论】:
【参考方案1】:见FileSecurity class in MSDN
以下代码示例使用 FileSecurity 类在文件中添加和删除访问控制列表 (ACL) 条目。您必须提供有效的用户或组帐户才能运行此示例。
using System;
using System.IO;
using System.Security.AccessControl;
namespace FileSystemExample
class FileExample
public static void Main()
try
string fileName = "test.xml";
Console.WriteLine("Adding access control entry for "
+ fileName);
// Add the access control entry to the file.
AddFileSecurity(fileName, @"DomainName\AccountName",
FileSystemRights.FullControl, AccessControlType.Allow);
Console.WriteLine("Removing access control entry from "
+ fileName);
// Remove the access control entry from the file.
RemoveFileSecurity(fileName, @"DomainName\AccountName",
FileSystemRights.FullControl, AccessControlType.Allow);
Console.WriteLine("Done.");
catch (Exception e)
Console.WriteLine(e);
// Adds an ACL entry on the specified file for the specified account.
public static void AddFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
// 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);
// Removes an ACL entry on the specified file for the specified account.
public static void RemoveFileSecurity(string fileName, string account,
FileSystemRights rights, AccessControlType controlType)
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
rights, controlType));
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
【讨论】:
【参考方案2】:如果有帮助,试试这个代码
public static bool CheckReadWriteAccces(string filePath, System.Security.AccessControl.FileSystemRights fileSystemRights)
FileInfo fileInfo = new FileInfo(filePath);
string str = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToUpper();
foreach (System.Security.AccessControl.FileSystemAccessRule rule in fileInfo.GetAccessControl().GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
if (str == rule.IdentityReference.Value.ToUpper())
return ((rule.AccessControlType == System.Security.AccessControl.AccessControlType.Allow) && (fileSystemRights == (rule.FileSystemRights & fileSystemRights)));
return false;
/// <summary>
/// Make a file writteble
/// </summary>
/// <param name="path">File name to change</param>
public static void MakeWritable(string path)
if (!File.Exists(path))
return;
File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly);
【讨论】:
【参考方案3】:答案在于 FileSecurity 类。
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity.aspx
【讨论】:
以上是关于以编程方式向 C# 中的文件添加安全权限的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式将视图添加到 Xamarin.Android C# 中的 GridLayout
使用 C# 以编程方式删除“包括来自此对象父级的可继承权限”复选框