ReadLinesAsync in UWP from absolute file path

2

I get error

The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)

My code is

public async void ReadFile()
    {     
        var path = @"F:\VS\WriteLines.xls";
        var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

        var file = await folder.GetFileAsync(path);
        var readFile = await Windows.Storage.FileIO.ReadLinesAsync(file);
        foreach (var line in readFile.OrderBy(line =>
        {
            int lineNo;
            var success = int.TryParse(line.Split(';')[4], out lineNo);
            if (success) return lineNo;
            return int.MaxValue;
        }))
        {
            itemsControl.Items.Add(line);
        }
    }

The error shows up at var file = await folder.GetFileAsync(path);

c#
uwp
uwp-xaml
asked on Stack Overflow Jun 6, 2016 by paddy • edited Jun 6, 2016 by Alexei Levenkov

2 Answers

2

You cannot read a file from an arbitrary location on disk in a UWP App. There are a couple of ways you can still accomplish your task:

  1. You can add the WriteLines.xls file to your project and set it's build action to Content and Copy to output Directory to Copy if newer. You can then access the file with the code you have by simply replacing the "path" value to:

var path = @"WriteLines.xls"

More details here

  1. If you need to be able to read any files from disk, you need to either use a FilePicker to select the file or copy the file in the Documents Folder and change the folder to:

var folder = KnownFolders.DocumentsLibrary;

More details here

answered on Stack Overflow Jun 6, 2016 by AlexDrenea
1

You are asking for file with absolute path from application's local folder - hence it throws that error as you provide path that includes drive name.

In general UWP is very restrictive on where/how you can get files from - I don't think you can get it from absolute path in the sample (app needs more permissions to get to similar places). You can try StorageFile.GetFileFromPathAsync.

Detailed info on locations app can access - UWP apps on Windows 10: File access permissions.

answered on Stack Overflow Jun 6, 2016 by Alexei Levenkov • edited Jun 6, 2016 by Alexei Levenkov

User contributions licensed under CC BY-SA 3.0