Read file from subfolder of application installati

2019-02-20 22:44发布

I have to read the text content from an .txt file, this file is located in app installed folder, in a subfolder, according to Microsoft docs, I am doing it like this:

 private async void readMyFile()
    {
        // Get the app's installation folder.
        StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

        // Get a file from a subfolder of the current folder by providing a relative path.
        string txtFileName = @"\myfolder\myfile.txt";

        try
        {
            //here my file exists and I get file path
            StorageFile txtfile = await appFolder.GetFileAsync(txtFileName);
            Debug.WriteLine("ok file found: " + txtfile.Path);

            //here I get the error
            string text = await FileIO.ReadTextAsync(txtfile);
            Debug.WriteLine("Txt is: " + text);
        }
        catch (FileNotFoundException ex)
        {
        }

    }

the error is:

    Exception thrown: 'System.IO.FileNotFoundException' in mscorlib.ni.dll
exception file not found: System.IO.FileNotFoundException: The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Smadshop.MainPage.<testExistsFile>d__8.MoveNext()

Have to notice that if I use the file without subfolder everything is working fine.

4条回答
别忘想泡老子
2楼-- · 2019-02-20 23:25

GetFileAsync will take relative path in form folder/fileName. You can also get folder first and than file or use GetItemAsync

StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// Get a file from a subfolder of the current folder
// by providing a relative path.
string image = @"Assets\Logo.scale-100.png";
var logoImage = await appFolder.GetFileAsync(image);
查看更多
\"骚年 ilove
3楼-- · 2019-02-20 23:29

I think you need to use:

string txtFileName = @".\myfolder\myfile.txt";

The dot in the filename represents the current folder. In you want to specify using relative paths, then @"\myfolder\myfile.txt" is not correct.

查看更多
Anthone
4楼-- · 2019-02-20 23:33

you can do it in other way, using URI :

using Windows.Storage;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///file.txt");

So in your case it will be:

StorageFile txtfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///myfolder/myfile.txt"));
查看更多
霸刀☆藐视天下
5楼-- · 2019-02-20 23:36

@"\myfolder\myfile.txt"; if its a network path should be @"\\myfolder\myfile.txt"; If its a local file it needs a drive letter ie.@"c:\myfolder\myfile.txt";

However the documentation for GetFileAsync shows a file in a subfolder would be @"myfolder\myfile.txt"

When you use a filename without a subfolder it will look in the current folder.

查看更多
登录 后发表回答