C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » File Operations »

Give the filesystem rights


Posted Date: 30 Aug 2009    Resource Type: Code Snippets    Category: File Operations
Author: Shunmuganathan MMember Level: Diamond    
Rating: 1 out of 5Points: 8



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



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Filesystem rights  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: File watcher system
Previous Resource: How to get the latestcreated / lastmodified file in a directory using c#
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use