0

Here is a snippet of the code I'm having issues with:

public OpenPage(string filePath, int? index, object jobject)
        {
            InitializeComponent();

            File.SetAttributes(filePath, FileAttributes.Normal); // Here is where error in title occurs
            var readText = File.ReadAllText(filePath); // Also occurs here
            
            DisplayAlert("Alert", readText, "Ok");      
        }

I'm creating a UWP Xamarin.Forms application which needs to read a file from a selected directory (anything on the C: drive). Although I'm getting the error in the title when I don't choose files from the local cache / AppData.

Looking at other similar posts in StackOverflow (such as Why is access to the path denied?) was great for learning more about the subject, although many questions regarding this error are outdated.

In the post above, one person said that a directory cannot be passed as File.ReadAllText()'s parameter.

Is there any work around to this? I need access to files on the C: drive. For reference, the filePath in the constructor when I was debugging was ""C:\Users\ianpc\Desktop\config file clones\te0_ptt_wog.json".

IanPC
  • 35
  • 6
  • https://stackoverflow.com/questions/33082835/windows-10-universal-app-file-directory-access – Jason Mar 22 '21 at 18:15

1 Answers1

1

Is there any work around to this? I need access to files on the C: drive. For reference, the filePath in the constructor when I was debugging was ""C:\Users\ianpc\Desktop\config file clones\te0_ptt_wog.json".

The problem is UWP app runs in sandbox environment, so we can't use System.IO namespace to access file with path directly.

For xamarin forms app, we suggest you use Windows Storage api to access file with path and before that, you need enable broadFileSystemAccess capability.

For example

public interface FileAccessInterface
{
    Task<string> GetFileText(string filePath);
}

Implementation

[assembly: Dependency(typeof(FileAccessInterfaceImplementation))]
namespace XamarinPicker.UWP
{
    public class FileAccessInterfaceImplementation : FileAccessInterface
    {
        public async Task<string> GetFileText(string filePath)
        {
            var stringContent = "";
            try
            {
                var file = await StorageFile.GetFileFromPathAsync(filePath);
               
                if (file != null)
                {
                    stringContent = await Windows.Storage.FileIO.ReadTextAsync(file,Windows.Storage.Streams.UnicodeEncoding.Utf8);
                }
            }
            catch (Exception ex)
            {

                Debug.WriteLine(ex.Message);
            }

            return stringContent;
        }
    }
}

Usage

var text = await DependencyService.Get<FileAccessInterface>().GetFileText(@"C:\Users\xxxx\Desktop\test.txt");

For more please refer this case reply.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • I'll give this implementation a go; thanks so much for the information! – IanPC Mar 23 '21 at 17:49
  • I'm actually getting an error: 'IAsyncOperation' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'IAsyncOperation' could be found (are you missing a using directive for 'System'?). Although at the top of my file I have a using System statement. I'll be looking into this problem on other posts but if you have a fix for this it'd be super useful! – IanPC Mar 24 '21 at 16:43
  • 1
    It looks you missed `System.Runtime.WindowsRuntime.dll` library, please refer this [document](https://stackoverflow.com/a/62381819/7254781) and add it manually. – Nico Zhu Mar 25 '21 at 01:35