I am trying to get the ownership of a protected file in C#.
System.Security.AccessControl.DirectorySecurity Sec = new DirectorySecurity(directory, AccessControlSections.All);
is not working for me. I get the error UnauthorizedAccessException. So I cannot change the owner of the file and I cannot edit my access rights. Does anyone know a way how to set the directory permissions?
It sounds like you (or the user that you're application is running under) doesn't have access to the directory that you're trying to get access to.
Mike O'Brien published a nice blog post solving my problem:
http://blog.mikeobrien.net/2009/11/taking-ownership-and-setting-admin.html
This allows me to take over ownership of any folder.
Related
Hi i have problem with permission(i think). Im trying to download a file from Mega.nz by MegaApiClient and when it trying to do that that problem appears:
"System.UnauthorizedAccessException: Access to the path '/storage/emulated/0/Download' is denied."
so i was trying different ways to do that like changing path folder or just simple create new file and write all content to that new file but nothing works.
of course i have permission added in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_USER_DICTIONARY" />
<usespermissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
var client = new MegaApiClient();
client.LoginAnonymous();
Uri fileLink = new Uri(Link);
INodeInfo node = client.GetNodeFromLink(fileLink);
client.DownloadFile(node, node.Name); //problem occurs here
client.Logout();
I think that you are writing to internal memory not external.
On the new Android you need to make the user accept the permission. So there most be a warning popup to allow write permission, location, etc.
Also you may try this: Android Dev Console support
The problem probably occurs since your device / emulator on which you test your App has API 23 or higher. This means that the manifest permissions are not enough and you need to add runtime permissions asking for writing files.
This link will explain it using xamarin and this is the official documentation.
A runtime permission basically displays the user a dialog which lets him decide whether or not the App is allowed to access for instance the internal storage of the device.
Oh and this article will also help you implementing it.
I came across a very anoying problem and it took me a while to solve it. Since 99% of the internet was telling me it was not possible and i could not find the answer on stackoverflow i decided to post it here.
I was trying to change the ownership of an folder/file in my windows machine. This is not much of a problem since DirectorySecurtiy has a nice function for it: "SetOwner()". I got this to work for my current account but i could not get it to work for someone else. Everytime i tried to grant someone else ownership i got this error: “The security identifier is not allowed to be the owner of this object”.
// Get folder or creates if not exists
DirectoryInfo dInfo = Directory.CreateDirectory(folderPath);
// Get user
IdentityReference user = new NTAccount(username);
// Set owner
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.SetOwner(user);
dInfo.SetAccessControl(dSecurity);
So this code does work if i want to grant myself ownership but not if i want to grant someoneelse ownership. What to do?
After searching the internet further I found the solution in this blog: https://fixingitpro.com/2011/07/08/set-owner-with-powershell-%E2%80%9Cthe-security-identifier-is-not-allowed-to-be-the-owner-of-this-object%E2%80%9D/
It is very strange and i dont know why but if you use unc pathing it will work.
UNC names identify network resources using a specific notation. These names consist of three parts: a host device name, a share name, and an optional file path.
These three elements are combined using backslashes: \host-name\share-name\file_path
So what used to work for myself only: D:\{folderName} had to be: \\{IP or servername}\d$\{foldername}. If you test the exact same code agian only than with a different path it will work. Would be nice if someone could explain me why routing back to your own computer does work by a normal path does not.
Dont forget to run your application as administrator
I am using Directory.Exists() in my windows service (that is programmed in C#, 3.5 framework)to check to see whether a particular directory exists in the drive. When I run in local machine it works fine, meaning I am able to access the directory.
But when I deploy the windows service on a Virtual Machine, and start the service, it is not able to find the directory even though the directory exists. The directory is mapped on as
Q: drive, Q:\\temp\\local\\ folder
But the windows services always returns false for the Directory.Exists().
However when I give C:\ drive in place of Q:\ it works, but does not work for a mapped drive. I have tried with the UNC path, and I have made sure the mapped drive have the administrative rights and infact the read, write and execute permission. But it still returns false.
Can anyone please tell me why? And how to resolve?
Make sure the drive is mapped under the same user as the Service is running. If you map the drive as user A, it is not automatically mapped for anyone else too.
Mapped drives are only restored during interactive login which services generally do not perform:
Map a network drive to be used by a service
Short version: You can't do it, use the full UNC path instead.
This is most probably a problem with privileges. Your Windows service is probably running under an account which doesn´t have enough privileges to access the network path.
This is a possible duplicate: Accessing mapped folder from a Windows Service written in C#
Another possible solution is to use impersonation, check it out:
http://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.90).aspx
UPDATE
Came to think of it;
Try changing the identity of the application pool to a user with the same rights as your user.
As #Sriram pointed out the Directory.Exists() method will fail if any error occurs. What sort of exception do you get if you try to access the path?
Eg (for both mapped and UNC in case there is something going on there):
DirectoryInfo diMapped = new DirectoryInfo(#"Q:\temp\local\folder");
DirectoryInfo diUNC = new DirectoryInfo(#"\\servername\fnsw\tmp\126");
Note: Assuming that the white space before 'folder' in your path is a typo?
Steps to troubleshoot
Try accessing the network path manually in "Run" [WindowKey + R]
Try to access your map drive i.e.: M:\
Make sure you are the account owner of the mapping (mapping should be done under your account)
Go to Property and see if "Run As Administrator" is unchecked.
Remove mapping and re-add the mapping.
Make sure available offline (or sync offline) is turned off and folder is available from another computer.
Hope this helps!
I ran my program in C# that suppose to create and write a file in C:\, but an error occurred saying that access to the path C:\mytextfile.txt is denied. Is there any code that will give the user permission in C:\?
Yes by using the manifest http://www.codeproject.com/Articles/66259/Requesting-Admin-Approval-at-Application-Start or run as different account which has sufficient rights
You need to run the code as an administrator. You don't need C# code for this, you need to change how you're running the code.
However, I suspect that you're trying to do something wrong. I'd guess that you're trying to store some data locally, and are using the root of C: for simplicity, when you should actually be using the standard libraries to locate the users folder or to get a temporary file.
e.g. Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
(for other folders, use Intellisense to look at the other SpecialFolder children.)
In my .net windows application am using a xml file .
But after installing the application by creating setup, while I am running that 'Access Denied' to that xml file message is shown.
I am using System.IO.File methods for doing file operations.
If any body knows the solution pls share.
Write access to program directory has been more and more restricted (starting with XP/Vista) since that is a secruity risk!
I would suggest to have the "base version" of that file readonly in your program directory...
Upon start of your app you check whether it is present in ApplicationData or LocalApplicationData or CommonApplicationData - (to get the real path at runtime use Environment.GetFolderPath). If it is not there then copy it there - in those locations your application has write access by default with no need for Administrator rights.
Program Files folder has limited access and can be modified by Administrator account. I would suggest you to save your xml file in *C:\Users\YourPCName\AppData\Local\YourAppFolder* .
This way you will be able to access and modify the file
I had this problem once so I used Isolated Storage and that fixed my problem. It may or may not work for you depending on the nature of your situation, but here's a quick tutorial on how to use it:
http://www.codeproject.com/KB/dotnet/IsolatedStorage.aspx