Click here to Skip to main content
15,884,821 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, I want to give full read and write access on a file created in my application,I have written code

C#
string directory = AppDomain.CurrentDomain.BaseDirectory + "CustomerPayment.xml";
           exporter.SetFilename(directory);
           exporter.SetFileType(PeachwIEFileType.peachwIEFileTypeXML);

           if (!File.Exists(directory))
           {
               File.Create(directory);
           }

           IdentityReference everyoneIdentity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
           DirectorySecurity dir_security = Directory.GetAccessControl(directory);
           FileSystemAccessRule full_access_rule = new FileSystemAccessRule(everyoneIdentity,FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None,AccessControlType.Allow);
           dir_security.AddAccessRule(full_access_rule);

           Directory.SetAccessControl(directory, dir_security);


but error is coming that "attempted to perform an unauthorized operation.

please help me, i want to give full read and write permission on my above xml file

What I have tried:

I tried to give full access control on a file created by my application
Posted
Updated 24-Nov-16 4:41am
Comments
Philippe Mori 24-Nov-16 10:23am    
Start by putting your file in All users documents...

Try adding settings in web.config
<configuration>
<system.web>
<identity impersonate="true">

 
Share this answer
 
Comments
Member 10192073 24-Nov-16 7:10am    
in app.config we need to add this, my window application it is
The first thing to do would be to use a directory that is accessible by all users. Typically, you should use a subdirectory on All users documents.

In fact, you should never store user data in Program Files!

An alternative would be to use Application data directory.

Those directories can easily be found using Environment.GetFolderPath function.

If you use Application data, you might want to adjust permission during the installation as I don't think you would be able to do that while running as a user without asking elevation. I don't think it is needed for All users documents (unless default security was messed up).
 
Share this answer
 
If this is an ASP.NET app, your code is running under a very limited account.

If this is a normal Windows Forms or WPF, or some other desktop app, the code is running AS THE USER THAT LAUNCHED IT.

In any case, your code cannot grant more permissions to something that it already has as the account the code is running under.
 
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