Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help. this code i used to deny delete operation on a file. but it is not working. plz help me. i want to deny delete operation. when some one delete file . it will give error message" Delete Access is denied".
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-Jun-12 20:22pm
v2
Comments
NeonMika 22-Jun-12 2:19am    
Does your user have admin rights?
Is any error thrown while using this methods?
pradeep rasara 22-Jun-12 2:24am    
yes user have admin rights..........
Sandeep Mewara 22-Jun-12 2:23am    
Not clear. Why to give a delete option at all when you don't want it to be done?
pradeep rasara 22-Jun-12 2:24am    
i am making a application of security....... i want to deny delete file and folder
Jim Jos 22-Jun-12 3:13am    
Could you tell me E is a local drive or a network drive.. If the user has admin rights in the local drive the permission are overrided. Deny will work only if there is no admin rights

1 solution

Have a look at the File.SetAccessControl method[^].
 
Share this answer
 
Comments
pradeep rasara 22-Jun-12 2:53am    
i am using the same it works for read/write/full control/mofify. but not working for delete.
http://msdn.microsoft.com/en-us/library/system.io.file.setaccesscontrol.aspx

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