Alexander Bruun
Alexander Bruun

Reputation: 277

Does File.Exists not work inside a UWP project?

File.Exists(filePath); works perfectly inside a console application, but when I do the same thing inside uwp it doesn't detect a file.

I have tried to put breakpoints on various methods and stepped into anything that could give me some information about the issue, but I'm getting no information at all no matter what i try.

Code from UWP app:

            string path = @"C:\Users\Name\Desktop\image.jpg";

                if (File.Exists(path))
                {
                    ProcessFile(path);
                }
                else if (Directory.Exists(path))
                {
                    ProcessDirectory(path);
                }

UWP: When it hits File.Exists i get a return value of false, and yes i know for a fact the image is where it is.

Console: When it hits File.Exists i get a return value of true, then goes onto the called method without any issues.

I'm expecting my code to find a File and pass the filePath into my method called "processFile".

Does UWP applications not have access to files outside of its LocalStorage or is it another issue that I'm not seeing?

Upvotes: 2

Views: 949

Answers (2)

PhonicUK
PhonicUK

Reputation: 13844

This is correct. UWP apps are sandboxed and cannot access files outside of LocalStorage in this way.

If you want to open a file on the users file system you have to use FileOpenPicker.PickSingleFileAsync or similar to prompt the user to pick a file, which you'll then be able to work with.

Further reading: Working with Files in UWP applications

Upvotes: 2

Martin Zikmund
Martin Zikmund

Reputation: 39072

UWP does not have direct access to files outside of the application folder and application data folder. That are the only two locations accessible via the System.IO APIs.

You can use StorageFile APIs to access more locations if you enable appropriate capabilities - like access to libraries or broadFileSystemAccess or use file/folder pickers. In particular, broadFileSystemAccess allows you to access the whole file system, but your app should have a good reason to do so (otherwise it will not pass the Microsoft Store certification process).

For more info see the Docs.

Upvotes: 5

Related Questions