Click here to Skip to main content
12,239,992 members (58,735 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 20:53pm
Edited 21-Jan-12 4:07am
ambarishtv10.5K
v4
Comments
pradeep rasara 21-Jan-12 3:18am
   
i wnt to make it in window application
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

Solution 3

  Permalink  
Comments
NeptuneHACK! 21-Jan-12 6:22am
   
this is the best solution i guess. thank you all any ways :)
Rate this: bad
 
good
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.

2)
You can use MessageBox

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

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
Web02 | 2.8.160426.1 | Last Updated 21 Jan 2012
Copyright © CodeProject, 1999-2016
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