Vas*_*Doe 2 c# file-handling win-universal-app
我必须从.txt文件中读取文本内容,该文件位于应用程序安装文件夹中的子文件夹中,根据Microsoft 文档,我这样做:
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)
{
}
}
Run Code Online (Sandbox Code Playgroud)
错误是:
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()
Run Code Online (Sandbox Code Playgroud)
必须注意,如果我使用没有子文件夹的文件,一切正常。
你可以用其他方式做到这一点,使用URI:
using Windows.Storage;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///file.txt");
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下,它将是:
StorageFile txtfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///myfolder/myfile.txt"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1239 次 |
| 最近记录: |