问题
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.
Add your .DOCX file to the Assets folder within your project and set the build action to "Content".
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