Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从应用程序安装文件夹的子文件夹中读取文件_C#_Win Universal App_File Handling - Fatal编程技术网

C# 从应用程序安装文件夹的子文件夹中读取文件

C# 从应用程序安装文件夹的子文件夹中读取文件,c#,win-universal-app,file-handling,C#,Win Universal App,File Handling,我必须从.txt文件中读取文本内容,该文件位于app installed文件夹中的一个子文件夹中,根据,我是这样做的: private async void readMyFile() { // Get the app's installation folder. StorageFolder appFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; //

我必须从
.txt
文件中读取文本内容,该文件位于app installed文件夹中的一个子文件夹中,根据,我是这样做的:

 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()
在mscorlib.ni.dll中引发异常:“System.IO.FileNotFoundException”
找不到异常文件:System.IO.FileNotFoundException:文件名、目录名或卷标语法不正确。(HRESULT的例外:0x8007007B)
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在Smadshop.MainPage.d_u8.MoveNext()上
必须注意,如果我使用没有子文件夹的文件,一切正常

@“\myfolder\myfile.txt”如果是网络路径,则应为
@“\\myfolder\myfile.txt”如果是本地文件,则需要驱动器号,即“c:\myfolder\myfile.txt”

但是,GetFileAsync的文档显示子文件夹中的文件将是
@“myfolder\myfile.txt”


当您使用没有子文件夹的文件名时,它将显示在当前文件夹中。

我认为您需要使用:

string txtFileName = @".\myfolder\myfile.txt";
文件名中的点表示当前文件夹。如果要指定使用相对路径,则
@“\myfolder\myfile.txt”
不正确。

将采用
文件夹/文件名形式的相对路径。您也可以先获取文件夹,然后再获取文件或使用


您可以用另一种方式,使用
URI

using Windows.Storage;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///file.txt");
因此,在您的情况下,它将是:

StorageFile txtfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///myfolder/myfile.txt"));

挑剔:
@“\myfolder\myfile.txt”
也是相对路径,只是从设备的根目录开始。。。从当前驱动器的根目录查看时,可能在desktop.Net(在OP情况下可能不是UWP)应用程序上工作。好的,可能不正确,但我可以使用
StorageFile txtfile=wait appFolder.GetFileAsync(txtFileName)获取文件但您的解决方案是:在mscorlib.ni.dll中发生了“System.ArgumentException”类型的异常,但未在用户代码WinRT信息中处理:找不到具有指定名称(.\myfolder\myfile.txt)的项。附加信息:参数不正确
@VasileDoe
GetFileAsync
的文档显示子文件夹中的文件将是
@“myfolder\myfile.txt”
是的,就是这样-如果您根据此注释进行编辑,我会将您的答案更改为正确答案
StorageFile txtfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///myfolder/myfile.txt"));