StorageFile 的 UWP CopyAsync 在发布版本中不起作用,抛出异常

UWP CopyAsync of StorageFile is not working in release build, throws exception

由于我的 UWP FullTrust 应用程序的第一个 运行,我需要在 C:// 驱动器中复制一个图标文件。下面是我的代码,它在 Debug 模式下工作正常,但在 Release 模式下不工作。

var packagePath = Package.Current.InstalledLocation;
var srcPath = Path.Combine(packagePath.Path, "Assets\Systray_icon.ico");

Windows.Storage.StorageFile storageFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(srcPath);

Windows.Storage.StorageFolder storageFolder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync("C:\");
await storageFolder.CreateFolderAsync("MyAppFolder", CreationCollisionOption.ReplaceExisting);

Windows.Storage.StorageFolder sf = await Windows.Storage.StorageFolder.GetFolderFromPathAsync("C:\MyAppFolder\");

await storageFile.CopyAsync(sf);

在Debug和Release模式下都创建了文件夹,但在release模式下复制抛出异常

The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

任何形式的帮助都将不胜感激。提前致谢。

问题已解决。在发布模式下,文件夹创建有时需要时间,因为由于文件夹创建我没有在异步等待中使用任务。所以我发布了可行的代码示例以供他人帮助。

public void IconCopy()
{
   LOG(LogLevel.Standard, $"[IconCopy][+] ");
   try
   {
       var packagePath = Package.Current.InstalledLocation;
       var srcPath = Path.Combine(packagePath.Path, "Assets\Systray_icon.ico");
       Windows.Storage.StorageFile storageFile = await Windows.Storage.StorageFile.GetFileFromPathAsync(srcPath);

       await CreateContextMenuIconFolder();

       Windows.Storage.StorageFolder sf = await Windows.Storage.StorageFolder.GetFolderFromPathAsync("C:\MyAppFolder\");
       await storageFile.CopyAsync(sf);
   }
   catch (Exception ex)
   {
      LOG(LogLevel.Error, $"[IconCopy] Exception occured : {ex.Message.ToString()}");
   }

   LOG(LogLevel.Standard, $"[IconCopy][-] ");
}

public async Task CreateContextMenuIconFolder()
{
  LOG(LogLevel.Standard, $"[CreateContextMenuIconFolder][+] "); 

  try
  {
      Windows.Storage.StorageFolder storageFolder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync("C:\");
      await storageFolder.CreateFolderAsync("MyAppFolder", CreationCollisionOption.ReplaceExisting);
  }
  catch(Exception ex)
  {
     LOG(LogLevel.Error, $"[CreateContextMenuIconFolder] Exception occured : {ex.Message.ToString()}");
  }

  LOG(LogLevel.Standard, $"[CreateContextMenuIconFolder][-] ");
}