简体   繁体   English

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

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

I need to copy an icon file in C:// drive due to the first run of my UWP FullTrust Application.由于第一次运行我的 UWP FullTrust 应用程序,我需要在 C:// 驱动器中复制一个图标文件。 Here below is my code, which is working properly in Debug mode but not works in Release mode.下面是我的代码,它在调试模式下正常工作,但在发布模式下不工作。

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

The folder has been created in both Debug and Release mode but due to copy throws an exception in release mode.该文件夹已在调试和发布模式下创建,但由于复制在发布模式下引发异常。

The system cannot find the file specified.该系统找不到指定的文件。 (Exception from HRESULT: 0x80070002) (来自 HRESULT 的异常:0x80070002)

Any kind of help will be appreciable.任何形式的帮助都将是可观的。 Thanks in advance.提前致谢。

The problem has been resolved.这个问题已经解决。 In release mode, folder creation was taking time sometimes as I didn't use Task in asynchronous await due to folder creation.在发布模式下,文件夹创建有时需要一些时间,因为由于文件夹创建,我没有在异步等待中使用任务。 So I'm posting workable code samples for others' help.因此,我发布了可行的代码示例以供其他人帮助。

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][-] ");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM