无法使用 UWP 应用程序访问我系统上的 Word 文档 [英] Not able to access the Word Document on my system using the UWP application

查看:67
本文介绍了无法使用 UWP 应用程序访问我系统上的 Word 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 UWP 应用(通用 Windows)编辑现有的 Word 文档.但由于某种原因,我收到文件不存在"错误.

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.

我尝试使用以下代码访问 word 文档:

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: '找不到文档'

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

推荐答案

根据评论部分的进一步说明,请参阅以下说明.

Based on further clarification in the comments section, please see the following instructions.

  1. 将 .DOCX 文件添加到项目中的 Assets 文件夹,并将构建操作设置为内容".

  1. Add your .DOCX file to the Assets folder within your project and set the build action to "Content".

为了写入对文件的任何更改,我们需要将其复制到包 LocalFolder 中,然后从那里访问它.

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
    }
}

您需要对此进行一些扩展,以确保文件仅复制到 LocalFolder 一次等,但您已了解基本概念.

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.

这篇关于无法使用 UWP 应用程序访问我系统上的 Word 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆