Description : We create the read access to the file. Following is the code to set the read acess to the randomly generated file.
Namespace part
using System; using System.IO; using System.Security.AccessControl;
Coding Part
class MainClass { static void Main(string[] args) { FileStream MyFileStream; string MyfileName = Path.GetRandomFileName(); using (MyFileStream = new FileStream(MyfileName, FileMode.Create)) { } SetRule(MyfileName, "Everyone", FileSystemRights.Read, AccessControlType.Deny);
try { MyFileStream = new FileStream(MyfileName, FileMode.Create); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { MyFileStream.Close(); MyFileStream.Dispose(); } }
static void AddRule(string MyfilePath, string Myaccount, FileSystemRights rights, AccessControlType MyAccesscontrolType) { FileSecurity MyfileSecurity = File.GetAccessControl(MyfilePath); MyfileSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, MyAccesscontrolType)); File.SetAccessControl(MyfilePath, MyfileSecurity); } static void SetRule(string MyfilePath, string Myaccount, FileSystemRights rights, AccessControlType MyAcesscontrolType) { FileSecurity fSecurity = File.GetAccessControl(MyfilePath); MyfileSecurity.ResetAccessRule(new FileSystemAccessRule(account, rights, MyAcesscontrolType)); File.SetAccessControl(MyfilePath, MyfileSecurity); } }
Code Explanation
1. Create the method AddRule 2. Create the method SetRule 3. Create the Random File Name and give the access rule to that file.
By Nathan
|
No responses found. Be the first to respond and make money from revenue sharing program.
|