文件名、目录名或卷标语法不正确.(异常来自HRESULT: 0x8007007B)

本文关键字:异常 HRESULT 0x8007007B 不正确 语法 文件名 | 更新日期: 2023-09-27 18:09:30

当代码寻找路径时,我收到这个错误,我不明白这是什么时候发生的,我认为我做得对。

代码:

 string newUri = ImageGalleryUri.Replace("ms-appdatalocal/", "");  //Replace this part of the string with a nonspace character.
        newUri = newUri.Replace("/", "''");
        newUri = newUri.Replace("%20", " "); //Replace the ASCII code for space for an actual space. For some reason I'm getting invalid character error with %20.
        StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
        StorageFile storageFile = await folder.GetFileAsync(newUri);
        DataPackage dp = new DataPackage();    //Create the DataPackage containing the clipboard content.
        dp.SetBitmap(RandomAccessStreamReference.CreateFromFile(storageFile));
        Clipboard.SetContent(dp);
        await successDialog.ShowAsync();

错误如下:newUri = ms-appdata:'local'Books'Assets'Recursos para el docente'Matematicas'9'Esp'1'0'Geometria_Page_04.png

StorageFile storageFile = await folder.GetFileAsync(newUri);

文件名、目录名或卷标语法不正确.(异常来自HRESULT: 0x8007007B)

不妨试试:

newUri = Uri.EscapeDataString(newUri);

代替:

newUri = newUri.Replace("%20", " ");

  1. 调试以检查字符串的实际值。
  2. Windows + R,然后复制字符串,看看它是否打开。
  3. 查看是否所有斜杠/反斜杠都被正确转义(我不认为在这里使用反斜杠是正确的)。
  4. 创建一个名为file.txt的文件,并尝试如下:

    使用Windows.Storage;

StorageFile file = await StorageFile. getfilefromapplicationuriasync ("ms-appdata:///local/file.txt");

  1. 尝试用你的字符串替换上面函数传递的参数。