2

Thank you for visiting.

Here is the issue I am facing and would appreciate some help.

I am trying to access a file in a .NET standard DLL. Everything works if I use the Windows.Storage namespace, in a test UWP app, like so:

Windows.Storage.StorageFile f = await StorageApplicationPermissions.FutureAccessList.GetFileAsync("TestPickedFileToken1");
        txt.Text = await Windows.Storage.FileIO.ReadTextAsync(f);

The real app is a MVVM UWP app (targeting Fall Creators Update) that accesses a .NETStandard 2.0 DLL. The problem is that I can't find the assembly that contains Windows.Storage namespace to add to my DLL. It is available in the UWP and when I try to use the following in System.IO it gives me an access to path denied error in the DLL:

string s = File.ReadAllText(filename);

I need to read/write to the file from the DLL. It will work if I read/write in the ViewModel of the UWP app but I don't want to do that. Is it possible add a reference to Windows.Storage in my DLL or is there an alternative to accessing a file in the DLL?

Thanks in advance for your help!

Vivek Verma
  • 333
  • 3
  • 13
  • @HansPassant I have no idea about UWP but that `StorageFile` API looks to me as if normal file access is not possible there and we have to use other means to interact with a (likely sandboxed) file system. And if this is a UWP-exclusive API, then that would mean that you could not use it from within a netstandard library. – poke Feb 15 '18 at 19:57
  • @Hans Passant, Thank you for your response. Latest UWP apps do not allow access to all locations of the file system without using Windows.Storage namespace and giving permissions to that location first. Earlier I was using a .NET Standard 1.4 DLL and a UWP app targeting 15063 and was not getting an error with System.IO.File.ReadAllText but thanks for trying. – Vivek Verma Feb 15 '18 at 20:01
  • Can those Storage APIs work with streams, so could just work with streams in your netstandard library? – poke Feb 15 '18 at 20:08

1 Answers1

1

Update

Information in this post is outdated. Please refer for example here.

Original answer

Unfortunately you cannot access any file path without the StorageFile API. When the user grants you access to a file using the FilePicker and you get the reference in form of a StorageFile, you still don't have access to that same path directly using System.IO classes.

At the same time you cannot use StorageFile APIs in a .NET Standard library. What you can do however is to get a System.IO.Stream from a StorageFile which you can then use in your .NET Standard library:

var readStream = await file.OpenStreamForReadAsync();
var writeStream = await file.OpenStreamForWriteAsync();

To access this extension method, make sure to add using System.IO; to the top of your source code file.

Community
  • 1
  • 1
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • 2
    Man, why does every answer I come across for anything UWP related start with "Unfortunately, you cant do that in UWP"? I'm getting really tired of hitting brick walls every step of the way with this technology. – Chris Lees May 28 '18 at 20:17
  • Actually this has changed! Check out this answer - https://stackoverflow.com/a/49854399/732221 – Martin Zikmund May 28 '18 at 20:28
  • Actually it's not changed at all, .NET Standard libraries is not allowed to use System.IO neither with broadFileSystemAccess, userDataSystem nor runFullTrust. – Aberro May 14 '20 at 08:28
  • @Aberro Actually the original answer was written before `broadFileSystemAccess` existed, so I updated it afterwards, as the situation did change - `broadFileSystemAccess` allowed us access to any files on disk using their path with `Windows.Storage` APIs – Martin Zikmund May 15 '20 at 19:01