我正在尝试让我的文本编辑器应用程序处理文件启动。 Microsoft 在此处提供了如何执行此操作的示例:
http://msdn.microsoft.com/en-us/library/windows/apps/hh452684.aspx
不幸的是,它在接收文件时停止,并且没有提供有关如何实际打开该文件的任何信息。
我可以成功处理激活的事件,并且最终得到文件的绝对路径。例如,
C:\Users\Rory\Documents\test.txt
Metro 应用无权访问绝对路径,除非在某些条件下。
- 如果用户通过文件选择器选择文件
- 如果应用之前访问过该文件并且路径已存储在 Windows.Storage.AccessCache 中
- 如果应用程序在启动时传递该文件。
即使在这种情况下适用第 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.
我已将应用程序包 list 设置为接受 txt 文件。
最佳答案
StorageFile
或 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.
});
}
}
// ...
}
关于javascript - 如何处理 Windows 应用商店/WinJS 应用程序中的文件激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19194936/