Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how can we give permisson in c# so that no one can delete file.

i am using this code but it does not work .any one please help me where i am doing wrong. it work for all like full control/read/write/modify. but it does not work for delete

C#
private void button1_Click(object sender, EventArgs e)
      {
          string fileName = @"E:\kkk.txt";

          WindowsIdentity ident = WindowsIdentity.GetCurrent();
          WindowsPrincipal user = new WindowsPrincipal(ident);

          AddFileSecurity(fileName, user.Identity.Name,
                    FileSystemRights.Delete, AccessControlType.Deny);
      }

public static void AddFileSecurity(string fileName, string account,
          FileSystemRights rights, AccessControlType controlType)
      {

          FileSecurity fSecurity = File.GetAccessControl(fileName);

          fSecurity.AddAccessRule(new FileSystemAccessRule(account,
              rights, controlType));

        File.SetAccessControl(fileName, fSecurity);

      }
Posted
Updated 21-Jan-12 3:07am
v4
Comments
pradeep rasara 21-Jan-12 3:18am    
i wnt to make it in window application

Use File.SetAccessControl[^] on the file.
 
Share this answer
 
Comments
pradeep rasara 21-Jan-12 3:49am    
But i have tried this it does not work.
pradeep rasara 21-Jan-12 4:25am    
actually it works for full control,read, write.......but does not work for delete.
Kim Togo 21-Jan-12 4:36am    
Under FileSystemRights Enumeration link,
There is a FileSystemRights.Delete that "Specifies the right to delete a folder or file."
pradeep rasara 21-Jan-12 4:39am    
ya i have try this but does not work,it work for all except delete. but i want to do with delete
Kim Togo 21-Jan-12 6:04am    
You can try to experiment with file access control in Windows Explorer, and have another user login that do not have the same access. When you found a combination that works. Use File.GetAccessControl to readout what types of access list is need.
 
Share this answer
 
Comments
NeptuneHACK! 21-Jan-12 6:22am    
this is the best solution i guess. thank you all any ways :)
use Message Boxes and

1) you can use modal popup extender,you need to use this in userControl.asmx file and then call it in your page.

a). Select The User Control by Right click on solution explorer
b). Call the extender on UserControls
c). use its directive on the page
d). call it by using Label.

2)
You can use MessageBox

Page.ClientScript.RegisterStartupScript(this.gettype(),"ok","javascript:alert('Don't Delete');",true);
 
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