从应用程序安装文件夹的子文件夹的文件阅读 [英] Read file from subfolder of application installation folder

查看:221
本文介绍了从应用程序安装文件夹的子文件夹的文件阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要读从 .TXT 文件中的文本内容,该文件位于应用程序安装文件夹,子文件夹,按的微软文档,我这样做是这样的:

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)
        {
        }

    }

该错误是:

    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.

推荐答案

你可以做其他的方式,使用 URI

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"));

这篇关于从应用程序安装文件夹的子文件夹的文件阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆