简体   繁体   English

无法使用UWP应用程序访问系统上的Word文档

[英]Not able to access the Word Document on my system using the UWP application

I am trying to edit a existing Word document using the UWP app (Universal Windows). 我正在尝试使用UWP应用(通用Windows)编辑现有的Word文档。 But for some reason I am getting "File does not exist" error. 但是由于某种原因,我收到“文件不存在”错误。

I have tried using the below code to access the word document: 我尝试使用以下代码访问word文档:

using(WordprocessingDocument wordDoc = WordprocessingDocument.Open("C:\\Users\\Public\\Desktop\\Doc1.docx", true))
{

}

System.IO.FileNotFoundException: 'Could not find document' System.IO.FileNotFoundException:'找不到文档'

By default, the UWP doesn't allow to access the files outside the app container. 默认情况下,UWP不允许访问应用程序容器外部的文件。 But from windows 10 build 17134, a new capability broadFileSystemAccess has been introduced. 但是从Windows 10内部版本17134开始,引入了一项新功能broadFileSystemAccess It allows apps to get the same access to the file system as the user who is currently running the app without any additional file-picker style prompts during runtime. 它允许应用程序与当前运行该应用程序的用户对文件系统具有相同的访问权限,而在运行时不会出现任何其他文件选择器样式的提示。

So, please check if you have declare this capability in the 'Package.appxmanifest' file. 因此,请检查您是否在'Package.appxmanifest'文件中声明了此功能。

Please see File access permissions and broadFileSystemAccess entry in App capability declarations for more information. 有关更多信息,请参见App功能声明中的 文件访问权限和broadFileSystemAccess条目。

If you still face this issue when you add the broadFileSystemAccess capability, then, the issue should be in 'WordprocessingDocument.Open' API. 如果在添加broadFileSystemAccess功能时仍然遇到此问题,则该问题应该在“ WordprocessingDocument.Open” API中。 You need to note that 'File access permissions' document has mentioned: 您需要注意,“文件访问权限”文档中提到:

This broadFileSystemAccess capability works for APIs in the Windows.Storage namespace. 这种broadFileSystemAccess功能适用于Windows.Storage命名空间中的API。

This means that the 'WordprocessingDocument.Open' may not use the Windows.Storage APIs to access the files. 这意味着'WordprocessingDocument.Open'可能不会使用Windows.Storage API来访问文件。 If so, you need to report this issue to Open-XML-SDK . 如果是这样,您需要将此问题报告给Open-XML-SDK

Based on further clarification in the comments section, please see the following instructions. 根据评论部分的进一步说明,请参阅以下说明。

  1. Add your .DOCX file to the Assets folder within your project and set the build action to "Content". 将.DOCX文件添加到项目内的Assets文件夹中,并将生成操作设置为“ Content”。

    将构建操作设置为内容

  2. In order to write any changes to the file, we'll need to copy it to the packages LocalFolder and then access it from there. 为了将任何更改写入文件,我们需要将其复制到包LocalFolder ,然后从那里访问它。

     var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/doc1.docx")); if (file != null) { //Copy .docx file to LocalFolder so we can write to it await file.CopyAsync(ApplicationData.Current.LocalFolder); String newFile = ApplicationData.Current.LocalFolder.Path + "/doc1.docx"; using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(newFile, true)) { //Your code here } } 

You'll need to expand on this somewhat to make sure the file is only copied to the LocalFolder once etc, but you get the basic idea. 您需要对此进行一些扩展,以确保仅将文件复制到LocalFolder一次,等等,但是您掌握了基本思想。

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

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