0

我正在尝试让我的文本编辑器应用程序处理文件启动。Microsoft 在此处提供了如何执行此操作的示例:

http://msdn.microsoft.com/en-us/library/windows/apps/hh452684.aspx

不幸的是,它在接收文件时停止并且没有提供有关如何实际打开所述文件的任何信息。

我可以成功处理激活的事件,并最终得到文件的绝对路径。例如,

C:\Users\Rory\Documents\test.txt

Metro 应用程序无权访问绝对路径,除非在某些情况下。

  1. 如果文件是由用户通过文件选择器选择的
  2. 如果应用之前访问过该文件并且路径已存储在 Windows.Storage.AccessCache 中
  3. 如果应用程序正在通过文件作为启动。

即使在这种情况下适用数字 3,我也无法打开文件。

我试过Windows.Storage.StorageFile.getFileFromPathAsync(path_to_file)了,但我收到了这个错误

0x80070005 - JavaScript runtime error: Access is denied.

WinRT information: Cannot access the specified file or folder (඀6). 
The item is not in a location that the application has access to (including 
application data folders, folders that are accessible via capabilities 
and persisted items in the StorageApplicationPermissions lists). Verify 
that the file is not marked with system or hidden file attributes.

我已经将我的应用程序包清单设置为接受 txt 文件。

4

1 回答 1

2

StorageFileor s在参数StorageFile中传递给您的应用程序WebUIFileActivatedEventArgs。试试这个:

app.onactivated = function (args) {
    if (args.detail.kind === activation.ActivationKind.file) {
        if (args.detail.files.size > 0) {
            var storageFile = args.detail.files[0];
            Windows.Storage.FileIO.readTextAsync(storageFile).then(function (text) {
                // Do something with the content of the file.
            });
        }
    }

    // ...
} 
于 2013-10-05T23:24:31.627 回答