Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Hello, I've been googleing this topic for some time now and can't seem to find anything useful. I'm writing an application where the user crates a (word) document from a template, the document is saved upon creation and is opened for the user to edit it. Once the user is done editing and saves the document (some other data is stored to the sql database and some other actions happen) the code must set read permissions to the document for this and some other user that are in the same department (according to some business logic).

So my question is how to programaticly set read permissions for this file in C# code?

Thank you for your answer!
B
Posted

Hi Please use this one to give programatically permission to access of file.

C#
FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Read, "C:\\test_r");
f2.AddPathList(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, "C:\\example\\out.txt");


Here you can set path of your file instead of "C:\\test_r"
 
Share this answer
 
v2
Comments
pykos 6-Jan-14 8:02am    
Thank you for your answer, but if I understand Microsoft right this gives the application that is using this file, permissions for reading and/or writing to this file. How ever I want to add the user that is using this application permissions to read or write.

For example, when I go to file properties->security the user should be listed there with only read permission. The problem is, when the users find out how to access these files (ie. they type the file path to the explorer) they must be denied access if they are not listed under the security tab.

I dont know maybe I'm looking at this all wrong...
BK 4 code 6-Jan-14 8:12am    
THen you should go for administrator rights.
pykos 6-Jan-14 8:20am    
Could you elaborate on that?
I'd use a different approach: move the file to a place where no user has access to. And if access is required, copy it to a position where the user can access it, afterwards remove it again.
 
Share this answer
 
v2
Comments
pykos 7-Jan-14 7:23am    
Thanks but I tested out my solution and it wokrs great... To add user rights to 200k documents takes about 20min wich is a great improvement over what we have now... thanks anyway
 
Share this answer
 
Comments
Dave Kreskowiak 7-Jan-14 7:32am    
You never said what type of application this is. The problem with your silicon is the your code runs as the user, not the system. This means that users cannot give themselves more permissions then they already have. Since they can apparently give themselves read and write permissIons to the file inside your code, they can also do it outside your application. Basically, this means your documents have no security at all!
pykos 7-Jan-14 7:45am    
This will be a webforms/mvc (havent decided yet) app that will run on the local intranet, and the document will reside on a shared disk, where users will have write permissions. After the user selects a document template, the file will be written on the shared disk, then the user will fill out the document and save it. After that the user will fill out the rest of the form and submit it. After submisson the app will remove all permissions on the document the user created, and rewrite them for this user and all others who need to be able to read this document. I assume that after all of this is done, the user will only have read permissions for this particular document, or am I wrong?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900