C#怎样做到禁止删除一个路径下的文件?答案满意有效不限量加分!!
我在代码中对文件设置这个属性没有用,只能做到不允许修改,但是删除就阻止不了了,大神看看我的代码该怎么写,FileSecurityds2=newFileSecurity()...
我在代码中对文件设置这个属性没有用,只能做到不允许修改 ,但是删除就阻止不了了,大神看看我的代码该怎么写,
FileSecurity ds2 = new FileSecurity();
ds2.AddAccessRule(new FileSystemAccessRule(Environment.UserName, FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Write, AccessControlType.Allow));
ds2.AddAccessRule(new FileSystemAccessRule("Administrator", FileSystemRights.Write | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Delete, AccessControlType.Allow));
File.SetAccessControl(文件的路径, ds2);
FileSecurity ds2 = new FileSecurity();
ds2.AddAccessRule(new FileSystemAccessRule(Environment.UserName, FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Write, AccessControlType.Deny);
ds2.AddAccessRule(new FileSystemAccessRule("Administrator", FileSystemRights.Write | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Delete, AccessControlType.Deny));
File.SetAccessControl(文件的路径, ds2); 展开
FileSecurity ds2 = new FileSecurity();
ds2.AddAccessRule(new FileSystemAccessRule(Environment.UserName, FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Write, AccessControlType.Allow));
ds2.AddAccessRule(new FileSystemAccessRule("Administrator", FileSystemRights.Write | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Delete, AccessControlType.Allow));
File.SetAccessControl(文件的路径, ds2);
FileSecurity ds2 = new FileSecurity();
ds2.AddAccessRule(new FileSystemAccessRule(Environment.UserName, FileSystemRights.Delete | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Write, AccessControlType.Deny);
ds2.AddAccessRule(new FileSystemAccessRule("Administrator", FileSystemRights.Write | FileSystemRights.DeleteSubdirectoriesAndFiles | FileSystemRights.Delete, AccessControlType.Deny));
File.SetAccessControl(文件的路径, ds2); 展开
1个回答
2015-08-09 · 知道合伙人软件行家
关注
展开全部
禁 止删除这个不太现实,你想想系统上有多少文件是不能删的(各种手段都可以)除了系统进程保护的除外。
另一种方式是加文件加到内存中,这样文件就不能删了。得有守护进程。
试试下面的。
FileSecurity fSecurity = File.GetAccessControl(fileName);
AuthorizationRuleCollection rules = fSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
foreach (AuthorizationRule rule in rules)
{
System.Security.Principal.NTAccount account =
(System.Security.Principal.NTAccount)rule.IdentityReference.Translate(typeof(System.Security.Principal.NTAccount));
if (account.Value != "BUILTIN\\Administrators")
{
fSecurity.AddAccessRule(new FileSystemAccessRule(account.Value, FileSystemRights.Delete, AccessControlType.Deny));
}
}
File.SetAccessControl(fileName, fSecurity);
另一种方式是加文件加到内存中,这样文件就不能删了。得有守护进程。
试试下面的。
FileSecurity fSecurity = File.GetAccessControl(fileName);
AuthorizationRuleCollection rules = fSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
foreach (AuthorizationRule rule in rules)
{
System.Security.Principal.NTAccount account =
(System.Security.Principal.NTAccount)rule.IdentityReference.Translate(typeof(System.Security.Principal.NTAccount));
if (account.Value != "BUILTIN\\Administrators")
{
fSecurity.AddAccessRule(new FileSystemAccessRule(account.Value, FileSystemRights.Delete, AccessControlType.Deny));
}
}
File.SetAccessControl(fileName, fSecurity);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询