Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
My requirement is to give write access to a particular folder for the user who have read access when running a specified application. I tried with google all the options gives only error like "Attempted to perform an unauthorized operation". I am working in c#
 colsole application. Please help on this issue


What I have tried:

I have tried with
C#
DirectoryInfo dInfo = new DirectoryInfo(FileName);
           DirectorySecurity dSecurity = dInfo.GetAccessControl();
           dSecurity.AddAccessRule(new FileSystemAccessRule(Account,
           Rights,
           ControlType));
           dInfo.SetAccessControl(dSecurity);
Posted
Updated 2-May-17 7:57am
Comments
ZurdoDev 2-May-17 6:48am    
You have to use an account that has permissions. I'm not sure what you are really asking.
sankarisiva 2-May-17 6:50am    
The requirement is, Run application from account which not having write permission.
Suvendu Shekhar Giri 2-May-17 6:51am    
Can you try running visual studio as administrator? not sure though...
sankarisiva 2-May-17 6:55am    
Actually some one running automation job who didn't have write access. I am going to add write access to that guy while he/she running automation. This is original requirement

To change the permissions of a directory the user needs permission to change permissions or be the owner of the directory. The "Change Permissions" permission is a special permission that requires the "Full Control" permission if not set explicitly.

If you want to give a user write access to a directory, you have to elevate privileges using the owner account of the directory or an adminstrative account.
 
Share this answer
 
Comments
sankarisiva 2-May-17 7:06am    
Shall I give directory ownership to the current user using owner name(admin username) by set owner
Jochen Arndt 2-May-17 7:25am    
I don't know your setup. But it is generally a bad idea to use non-restrictive permissions.

If possible you should try to set the write permission before executing your application.

Otherwise you have to execute your application as the user owning the directory or as administrator.
sankarisiva 2-May-17 7:54am    
Can you help me on dynamically changing ownership of a directory using c# code
Jochen Arndt 2-May-17 7:58am    
Why do you want to change the ownership using a C# application?
That would require the application to be executed as the current owner too.

Changing permissions is an adminstrative task that must be done by an adminstrator or the owner.
A user cannot give themselves more permissions than they already have. This is especially true for a resource (directory) they do not own.

Normal users also cannot take ownership of a resource they do not already own unless they are an admin on the machine/domain the resource resides.

You "requirement" isn't a requirement. It's a complete misunderstanding of how Windows Security works. You have to learn about security first before you call that a "requirement".
 
Share this answer
 
Comments
sankarisiva 2-May-17 10:19am    
Thanks for your answer. This answer will be very helpful to prove this won't be implemented to some of my friends who requested this question.
The security descriptor of a folder contains a list of SID (users, groups and, when joined to a domain, computers) where access is allowed or denied.

On the other hand, a process has a token associated. The token contains the "launcher" user's SID and groups it belongs. You can temporarily change a process' thread token with impersonation APIs.

In order to reach your goal, the better solution is to give the desired access to a new user (or use an existing one) and launch the process with that user's credentials.
 
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