【问题标题】:Open a file in Windows 8 Metro App C#在 Windows 8 Metro App C# 中打开文件
【发布时间】:2012-09-17 10:26:11
【问题描述】:

我有一个包含 文件路径 的数组(例如“C:\...”),我想从我的应用程序中使用默认应用程序打开它们。假设它是一个列表,当我单击其中一个时,它会打开。

这是启动文件异步的方式:

await Windows.System.Launcher.LaunchFileAsync(fileToLaunch);

它需要Windows.Storage.StorageFile 类型的文件,它具有Path 只读属性,所以我无法设置路径。轻按/点击后如何打开它们?

【问题讨论】:

标签: c# microsoft-metro


【解决方案1】:

复制自我在 cmets 中的链接:

// Path to the file in the app package to launch
   string exeFile = @"C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe";

   var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(exeFile);

   if (file != null)
   {
      // Set the option to show the picker
      var options = new Windows.System.LauncherOptions();
      options.DisplayApplicationPicker = true;

      // Launch the retrieved file
      bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
      if (success)
      {
         // File launched
      }
      else
      {
         // File launch failed
      }
   }

您当然可以省略 var options = **-Part,这样 ApplicationPicker 就不会被打开

或者你可以使用这个:

StorageFile fileToLaunch = StorageFile.GetFileFromPathAsync(myFilePath);
await Windows.System.Launcher.LaunchFileAsync(fileToLaunch);

【讨论】:

    【解决方案2】:

    你应该使用这个方法 http://msdn.microsoft.com/en-US/library/windows/apps/windows.storage.storagefile.getfilefrompathasync
    关于类型 StorageFile

    如果您已经有path,则使用此方法获取文件

    【讨论】:

      【解决方案3】:

      答案就在这个示例中:http://code.msdn.microsoft.com/windowsapps/Association-Launching-535d2cec

      简短的回答是:

      // First, get the image file from the package's image directory.
      string fileToLaunch = @"images\Icon.Targetsize-256.png";
      var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileToLaunch);
      
      // Next, launch the file.
      bool success = await Windows.System.Launcher.LaunchFileAsync(file);
      

      【讨论】:

        【解决方案4】:

        最好的解决办法是这样的:

        string filePath = @"file:///C:\Somewhere\something.pdf";
        
            if (filePath != null)
            {
               bool success = await Windows.System.Launcher.LaunchUriAsync(new Uri(filePath));
               if (success)
               {
                  // File launched
               }
               else
               {
                  // File launch failed
               }
            }
        

        该应用程序使用系统默认应用程序启动它,在本例中使用 Adob​​e Reader。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-08
          • 1970-01-01
          相关资源
          最近更新 更多