I'm writing a Windows 8.1 store application and am trying to create a subfolder in the ApplicationData.Current.RoamingFolder directory. There is no data currently stored there. However, the following line throws a System.UnauthorizedAccessException saying "Access is Denied".
var folder = await ApplicationData.Current.RoamingFolder.CreateFolderAsync("subfolder", CreationCollisionOption.OpenIfExists);
Strangely enough, if I use LocalFolder instead of RoamingFolder, the operation succeeds.
var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("subfolder", CreationCollisionOption.OpenIfExists);
Why can't I create a subfolder in the roaming folder? I can manually create the folder via Windows Explorer.
The documentation (link) says that there are limitations on what can be placed in the folder, specifically:
"The sync engine has restrictions on the file name conventions that you must follow to ensure the items in the roaming folder can roam. Be sure that your file and folder names do not contain leading whitespace. The sync engine may limit the total size of settings and files that can roam."
However, I don't see anything that would explain the above exception. What am I missing and what other limitations am I missing about the roaming folder?
Interestingly, if I create a brand-new application both lines of code function perfectly. I don't know why that is.
Edit: As mentioned by Nate, the CreateFolderAsync documentation says "If you try to create a subfolder in a virtual folder like a library or a file group, this method may fail." Does anyone know if that applies here? If it does, what limitations does it introduce? The only information I've found on this issue is this question, but it doesn't seem to firmly resolve the issue.
Related
I want to use Directory.GetDirectories(path) and then working with sub-directories but, when I start running my app, VS return me System.UnauthorizedAccessException. I can understand I fix this, no problem. The real problem is, as you can see in the linked picture, code try to acces a folder that doesn't exist.
In windows explorer I show all hide files and folder, and as you can see (again) when I go to the right spot in my PC, "Ma Musique" doesn't exist (and never existed by the way).
Where is folder come from and how can I deal with this ?
EDIT :
Code try to access this directory just after Lumion directory. As you see above, "Ma Musique" doesn't exist.
CMD (as administrator) doesn't find it too.
Directory.Exists() return true when give it "Ma musique" directory path.
That is what is known as a "virtual" folder and it is there for backwards compatibility with old versions of Windows. When users got their own music/video/etc folders, they were all turned into system virtual folders that don't actually exist at all as they are aggregates of content in multiple folders. However programs that expect these folders to exist would break if they were missing so Windows would make a symbolic link that in some cases would allow a redirect. I don't remember for sure but i think the redirect only worked if you were running as administrator. In any case you can't browse into them as they aren't real.
You are seeing "acces denied" and/or do get this Exception "System.UnauthorizedAccessException".
Microsoft has a tool ICACLS that displays or modifies discretionary access control lists (DACLs) on specified files, and applies stored DACLs to files in specified directories.
Because of the mentioned error, you should investigate the response of:
icacls "C:\Users\thoma\Documents\Ma musique"
(When you get an error about "Access denied", please execute this as Administrator)
The output of ICACLS should explain why you do no have access to that file/directory.
I'm trying to save a file without a filepicker under my desktop or somewhere else, but I always get the error "Access to path [...] is denied"
This is my code:
private async void SaveFile(string Path)
{
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(Path);
StorageFile sampleFile = await folder.CreateFileAsync("sample.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
}
I tried the same code but with the Localfoder and it worked.
Is there any way to get access to the path without a filepicker?
Using FilePicker gets permission from the user to access file locations. File system access is a restricted capability in UWP. You can enable Broad Filesystem Access in your package manifest to get this permission when the app is installed. It's not recommended for apps published to the Microsoft Store, because it makes it less likely for you app to be approved. See the App capability declarations documentation.
Alternatively, you could run your app from a folder that contains the desktop in one of its subfolders by using the AppExecutionAlias extension mention under Accessing additional locations in File Access Permissions. Underneath that section there is also an explanation of Broad Filesystem Access as well.
There is a shared network folder at \\FILESERVER\MyFolder. The security principal Everyone has Full Control on This folder, subfolders and files. There are no DENY permissions on the folder. There are three users, and all of them can use File Explorer to enter the directory and create a file named test.txt.
This code runs fine for two of the users:
string testLocation = #"\\FILESERVER\MyFolder\";
string testFullPath = Path.Combine(testLocation, "test.txt");
Directory.CreateDirectory(testLocation);
bool exists = Directory.Exists(testLocation); // RETURNS TRUE!
File.Create(testFullPath); // For one user, this raises:
// System.IO.DirectoryNotFoundException: Could not find part of the path
// '\\FILESERVER\MyFolder\test.txt'
So Directory.CreateDirectory runs fine (the folder already exists), but File.Create seems to think the directory does not exist. As that user, I can type in \\FILESERVER\MyFolder in Explorer, see the existing files there and create new files, no problem. But not when running the code above.
The program's process is running under the user in question.
This only happens with network paths, not local paths. If I change \\FILESERVER\MyFolder to C:\Users\Username\Desktop, the file is created.
The background for the problem is that the program fails to export an XLSX file to the network folder for this specific user. In this case, I attempted to work around the problem after days of researching and not finding anything: If the file fails to export to the network folder, the program exports it to Desktop and then attempts to move it to the network folder. Of course, the program fails when moving. I narrowed it down to the snippet shown above. The code shown above is identical to the code that fails, except that I've changed the name of the server and folder for the example.
Any ideas?
Renaming the directory worked. Instead of \\FILESERVER\MyFolder, the folder is now named \\FILESERVER\MyFolderNew, and it's working. If I rename the folder back to MyFolder, it stops working again.
I don't have time to dig deeper. There are no firewall/antivirus rules regarding this directory, that's all I know. Renaming the folder works for us.
I'm basically creating a directory in MyDocuments. I think it's worth mentioning that MyDocuments at my company is a network path.
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path = Path.Combine(path, string.Format(#"A\XXX\C\{0:yyyyMMdd_HHmmss}", startTime));
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
The issue here is quite funny. It was working fine for 2 years and recently I cleaned manually path in subdirectory 'XXX'. Now it doesn't work. If I change 'XXX' to anything else it works. It looks like something is wrong with this particular directory. I can delete that directory manually and recreate it in windows explorer. It happens when running from VS2015 and from exec as well. Any ideas?
Edit:
Just to clarify:
Exception thrown: "Could not find a part of the path"
In Line Directory.CreateDirectory(path);
Edit:
The reproduce in windows explorer:
I can create and remove XXX and C this is fine. When I tried to create any subdirectory in C windows explorer hangs. So I removed the entire A directory and now when I try to create it manually I can't recreate A because it says that the folder or a file in it is open in another program. How is it possible if it doesn't exist.
The answer is pretty simple. In your company we use sync center to synchronize data on this shared drive. The root directory wasn't fully synchronized so whenever I tried to create directory it thrown an error. After synchronizing and deleting it from my mirrored version and sync center version I could create it again.
When I try to rename a Directory using the following code:
try
{
System.IO.Directory.Move(oldPath, newPath);
}
catch (System.IO.IOException e2)
{
Console.WriteLine(e2.Message);
}
I get the following exception: The process cannot access the file because it is being used by another process.
I don't understand why it says "file" in the first place.
Also, the directory is empty. What file is it referring to?
Lastly, how to manage to rename the Directory without any exceptions?
UPDATE: I guess I found the reason for the exception, it is because I am trying to rename the file/folder names of files/folders situated in the Google Drive. The Google Drive application is the other process using it! Any solutions to rename a folder in the Google Drive? But the weird thing is that I don't get this exception when I try to rename files located in the Google Drive through C#.
Thanks!
Your folder seems to be in use by another process. Try to close your explorer or other programs, that use that folder. If nothing help - try to restart your machine. If those won't help - consider using Unlocker to free folder from usage of another process. Note that it would be weird, if non-system folder is occupied after restart of the machine
It says "file" because the underlying commands are probably using a
generic move command for both directories and files and the
localized error string contains the word file.
Even if a directory is empty, it can be locked for any edits if you have it "open" in an application (do you have explorer Windows open in the directory, is the command prompt current directory set to the one you want to move/delete)?
Unfortunately there's not much you can do unless you want to start killing offending processes programmatically.
You can use Process Explorer to find the process that has a lock on that folder. See http://windowsxp.mvps.org/processlock.htm for more info.
1 Verify that newPath already does not exist
http://msdn.microsoft.com/en-us/library/system.io.directory.move.aspx
2 Verify that your directory does not contain opened file