从应用程序安装文件夹的子文件夹的文件阅读

编程入门 行业动态 更新时间:2024-10-27 05:21:09
本文介绍了从应用程序安装文件夹的子文件夹的文件阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要读从 .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"));

更多推荐

从应用程序安装文件夹的子文件夹的文件阅读

本文发布于:2023-11-12 17:03:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582041.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   应用程序   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!