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

走远了吗. 提交于 2021-02-10 07:54:10

问题


I am trying to edit a existing Word document using the UWP app (Universal Windows). But for some reason I am getting "File does not exist" error.

I have tried using the below code to access the word document:

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

}

System.IO.FileNotFoundException: 'Could not find document'


回答1:


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".

  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.

    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.




回答2:


By default, the UWP doesn't allow to access the files outside the app container. But from windows 10 build 17134, a new capability broadFileSystemAccess has been introduced. 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.

Please see File access permissions and broadFileSystemAccess entry in App capability declarations for more information.

If you still face this issue when you add the broadFileSystemAccess capability, then, the issue should be in '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.

This means that the 'WordprocessingDocument.Open' may not use the Windows.Storage APIs to access the files. If so, you need to report this issue to Open-XML-SDK.



来源:https://stackoverflow.com/questions/56366569/not-able-to-access-the-word-document-on-my-system-using-the-uwp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!