Fables Alive
Fables Alive

Reputation: 2808

is it possible to access files in local directories? (Universal Windows Platform)

I'm trying to understand how to access local files rather than ms-appx resources etc. But it seems it's not possible. Did I missing something? This small example throws an error but the file does exist in root of drive D. Is it possible or not in Universal Windows Platform development? Or maybe it just doesn't work when debugging on a PC, but maybe on real device windows IOT?

private void Button_Click(object sender, RoutedEventArgs e)
{
    string path = @"D:\skyblue.jpg";
    Uri uri_ = new Uri(path, UriKind.Absolute);

    var a=Windows.Storage.ApplicationData.Current.LocalFolder;

    Task.Run(() =>{
        if (!File.Exists(path))
         Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
        {
            textBlock1.Text = "file not found";//this happens albeit file exists
        });
    });
}

Upvotes: 2

Views: 848

Answers (3)

awsnap
awsnap

Reputation: 13

Are you trying to store a file or access a resource? It's unclear your end goal here. You also don't show how you create path. It may help to write this string out to verify it's where you think it is.

In some of my apps I use _PathToPubCacheFolder = ApplicationData.Current.GetPublisherCacheFolder("YourInfoHere").Path; to store a file.

https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions is a good reference.

Upvotes: 0

Rita Han
Rita Han

Reputation: 9700

You need use File.Exists() to check the path of a file like this:

        string path = @"D:\test\1.jpg";
        await Task.Run(() => {
            if (!File.Exists(path))
                throw new FileNotFoundException();
        });

And use Directory.Exists() to check the path of a directory like this:

        path = @"D:\test";
        await Task.Run(() => {
            if (!Directory.Exists(path))
                throw new FileNotFoundException();
        });

You can find detailed information here.

For accessing Application data locations there are two methods:

        // Method #1       
        string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Project1", "1.jpg");

        StorageFile file = await StorageFile.GetFileFromPathAsync(path);

        // Method #2
        StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/Project1/1.jpg"));

Note:

  1. You need to Set folder permissions for UWP apps. For example, run this command on Windows IoT Core device: FolderPermissions d:\test -e
  2. You need to declare file type like this:

enter image description here

Upvotes: 2

Wellspring
Wellspring

Reputation: 1340

https://channel9.msdn.com/Events/Build/2017/B8012

As of creator's update, at minute 32:45 or so in that video, they confirm that the answer is "none" -- and one of the things they note you cannot do is "direct access to file system." That was some months ago, and maybe you're working with a more recent version. But as a starting point, I think it's safe to say that file access, if it's there at all, is a new feature. I haven't sat down to see what's possible in the most recent releases, but if you're not at the very cutting edge, safe to say you cannot access the file system?

Upvotes: 1

Related Questions