Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to create an app which remove read and write permission of a file,using C#. how can i do it?

additional information moved from comment below [Nelek]
C#
using System.Security.AccessControl;
try
{
   FileStream oFileStreamDec = new FileStream(@"H:\Tripz\Testi\6.jpg", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
   // 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("SYSTEM", System.Security.AccessControl.FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
System.IO.File.SetAccessControl(@"H:\Tripz\Testi\6.jpg", oFileSecurity);
}
catch
{
   MessageBox.Show("Error");
}
Posted
Updated 6-Nov-14 10:28am
v4
Comments
Richard MacCutchan 6-Nov-14 13:28pm    
You do it by writing code. Exactly which part of this program are you unable to do?
Richard Deeming 6-Nov-14 13:30pm    
For NTFS permissions, start with the System.Security.AccessControl.FileSecurity class[^].
ZurdoDev 6-Nov-14 14:03pm    
Do you have a question?
Anuruddha_r 6-Nov-14 14:17pm    
i use below code to remove read write permission of a file How Can I Give W/R permission Back
Richard Deeming 6-Nov-14 14:32pm    
Don't post your code as an answer to your question. Use the "Improve question" link instead.

Hang on. The question no one is asking is WHY??

If the user running your code, which runs AS THE USER!!, has the permissions to remove/add those permissions, they also have the permissions to add them back to the file WITHOUT YOUR APP just by doing it in Explorer.

Your attempt at "security" is pointless.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 6-Nov-14 16:12pm    
Best answer so far (and the only reasonable one). My 5.
—SA
BillWoodruff 7-Nov-14 2:17am    
Actually, Dave, I asked "why" one hour before you posted this "solution," but, I asked in a comment.

Not that I'm saying that saying there's not a solution isn't a solution :)
Why not first go and read some blog posts and already written solutions for this?

First of all, go and read the Access Control[^] by Microsoft. Secondly, you can learn the FileIOPermission class from the MSDN[^]. They will provide you with minimal (but probably maximum) information related to your project.

The second link (MSDN link) has the detailed information for the permissions that you can set to the File using that code.
 
Share this answer
 

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