我正在尝试使用 UWP 应用程序(通用 Windows)编辑现有的 Word 文档。但由于某种原因,我收到“文件不存在”错误。
我尝试使用下面的代码来访问word文档:
using(WordprocessingDocument wordDoc = WordprocessingDocument.Open("C:\\Users\\Public\\Desktop\\Doc1.docx", true))
{
}
System.IO.FileNotFoundException:“找不到文档”
根据评论部分的进一步说明,请参阅以下说明。
-
将 .DOCX 文件添加到项目内的 Assets 文件夹中,并将构建操作设置为“Content”。
-
为了将任何更改写入文件,我们需要将其复制到包中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
}
}
您需要对此进行一些扩展,以确保该文件仅复制到LocalFolder
一次等等,但你明白了基本的想法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)