【问题标题】:Read file from subfolder of application installation folder从应用程序安装文件夹的子文件夹中读取文件
【发布时间】:2016-05-27 10:05:26
【问题描述】:

我必须从.txt 文件中读取文本内容,该文件位于应用程序安装文件夹中的子文件夹中,根据Microsoft docs,我这样做是这样的:

 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()

必须注意,如果我使用没有子文件夹的文件,一切正常。

【问题讨论】:

    标签: c# win-universal-app file-handling


    【解决方案1】:

    您可以使用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"));
    

    【讨论】:

      【解决方案2】:

      我认为你需要使用:

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

      文件名中的点代表当前文件夹。如果你想指定使用相对路径,那么@"\myfolder\myfile.txt" 是不正确的。

      【讨论】:

      • Nitpick: @"\myfolder\myfile.txt" 也是相对路径,只是从设备的根目录... 从当前根目录查看时,可能适用于桌面 .Net(在 OP 情况下可能不是 UWP)应用程序开车。
      • 好的,可能不正确,但我可以使用StorageFile txtfile = await appFolder.GetFileAsync(txtFileName); 获取文件,但您的解决方案是告诉我An exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll but was not handled in user code WinRT information: An item cannot be found with the specified name (.\myfolder\myfile.txt). Additional information: The parameter is incorrect
      【解决方案3】:

      GetFileAsync 将采用folder/fileName 形式的相对路径。您也可以先获取文件夹然后再获取文件或使用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);
      

      【讨论】:

        【解决方案4】:

        @"\myfolder\myfile.txt";如果它的网络路径应该是@"\\myfolder\myfile.txt";如果它是一个本地文件它需要一个驱动器号ie.@"c:\myfolder\myfile.txt";

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

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

        【讨论】:

        • @VasileDoe GetFileAsync 的文档显示子文件夹中的文件将是 @"myfolder\myfile.txt"
        • 是的,就是这样-如果您根据此评论进行编辑,我会将您的答案更改为正确的答案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-04
        • 2013-05-30
        相关资源
        最近更新 更多