以编程方式为 C# 中的文件添加安全权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1036571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically adding security permissions to files in C#
提问by jumbojs
In an asp.net app, I have a task that ftps some xml files down to a local folder on my computer. I then want to read those files but when they're copied to my local folder, they don't have the Network Service user account set up. So, my question is how, in .Net C#, do you programmatically add the "Network Service" account with full control to my xml files.
在 asp.net 应用程序中,我有一个任务,将一些 xml 文件 ftps 到我计算机上的本地文件夹。然后我想读取这些文件,但是当它们被复制到我的本地文件夹时,他们没有设置网络服务用户帐户。所以,我的问题是,在 .Net C# 中,您如何以编程方式将“网络服务”帐户添加到我的 xml 文件中并完全控制。
回答by pedrofernandes
try this code if help
如果有帮助,请尝试此代码
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);
}
回答by Jonx
The answer lies in the FileSecurity Class.
答案在于 FileSecurity 类。
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity.aspx
http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity.aspx
回答by Christopher Scott
See the FileSecurity class in MSDN
The following code example uses the FileSecurity class to add and then remove an access control list (ACL) entry from a file. You must supply a valid user or group account to run this example.
下面的代码示例使用 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);
}
}
}