对单个文件设置安全性?

发布于 2024-12-10 15:27:04 字数 847 浏览 0 评论 0原文

我试图在单个文件上实现安全性,以防止访问该文件或删除它,这里是代码:

//Create file
FileStream oFileStreamDec = new FileStream(@"C:\Decrypted_AMS.cfg", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
oFileStreamDec.Write(DecryptedXML, 0, DecryptedXML.Length);
//Create access rules
FileSystemAccessRule oAccessRuleFullControl = new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name, FileSystemRights.FullControl, AccessControlType.Allow);
//Create file security and apply rules to it
FileSecurity oFileSecurity = new FileSecurity(@"C:\Decrypted_AMS.cfg", AccessControlSections.All);
oFileSecurity.AddAccessRule(oAccessRuleFullControl);
//Here is the problem !!!!!!!
oFileStreamDec.SetAccessControl(oFileSecurity);
oFileStreamDec.Close();

我尝试在关闭流并设置访问控制后再次打开该文件,但出现了同样的问题,我已经尝试过也它在文件流以外的普通文件上,也一样,我有一个具有所有权限的管理员帐户,那么问题是什么以及如何解决它?

I am trying to implement a security on a single file to prevent either accessing the file or deleting it here is the code :

//Create file
FileStream oFileStreamDec = new FileStream(@"C:\Decrypted_AMS.cfg", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
oFileStreamDec.Write(DecryptedXML, 0, DecryptedXML.Length);
//Create access rules
FileSystemAccessRule oAccessRuleFullControl = new FileSystemAccessRule(WindowsIdentity.GetCurrent().Name, FileSystemRights.FullControl, AccessControlType.Allow);
//Create file security and apply rules to it
FileSecurity oFileSecurity = new FileSecurity(@"C:\Decrypted_AMS.cfg", AccessControlSections.All);
oFileSecurity.AddAccessRule(oAccessRuleFullControl);
//Here is the problem !!!!!!!
oFileStreamDec.SetAccessControl(oFileSecurity);
oFileStreamDec.Close();

I have tried to open the file again after closing the stream and setting the access control but same problem occurred, i have tried also it on a normal file other than a file stream and the same too, I have an admin account with all permissions so What is the problem and how to Solver it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天冷不及心凉 2024-12-17 15:27:04

试试这个,

FileStream oFileStreamDec = new FileStream(@"C:\Decrypted_AMS.cfg", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
oFileStreamDec.Write(DecryptedXML, 0, DecryptedXML.Length);
// Close the File first
oFileStreamDec.Close();
//Create file security and apply rules to it
FileSecurity oFileSecurity = new FileSecurity();
oFileSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule("Everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
System.IO.File.SetAccessControl(@"C:\Decrypted_AMS.cfg", oFileSecurity);

try this,

FileStream oFileStreamDec = new FileStream(@"C:\Decrypted_AMS.cfg", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
oFileStreamDec.Write(DecryptedXML, 0, DecryptedXML.Length);
// Close the File first
oFileStreamDec.Close();
//Create file security and apply rules to it
FileSecurity oFileSecurity = new FileSecurity();
oFileSecurity.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule("Everyone", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
System.IO.File.SetAccessControl(@"C:\Decrypted_AMS.cfg", oFileSecurity);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文