Click here to Skip to main content
12,897,242 members (49,073 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
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

  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 20-Jan-12 19:53pm
Updated 21-Jan-12 3:07am
pradeep rasara 21-Jan-12 3:18am
i wnt to make it in window application
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use File.SetAccessControl[^] on the file.
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.
pradeep rasara 23-Jan-12 7:11am
but it also do not work through Window explorer. I tried my best.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

NeptuneHACK! 21-Jan-12 6:22am
this is the best solution i guess. thank you all any ways :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

You can use MessageBox

Page.ClientScript.RegisterStartupScript(this.gettype(),"ok","javascript:alert('Don't Delete');",true);

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170424.1 | Last Updated 21 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100