c# - 将Word文档转换为base64字符串C#

标签 c# .net-4.0 ms-word vsto

我正在尝试获取事件 Word 文档的 base64 表示,同时它仍在 Word 中打开 并在 ReadAllBytes() 中收到以下错误:

进程无法访问另一个进程正在使用的文件“文件路径”

public string GetEncodedTemplate()
        {
            //Convert a Word document's base64 representation
            string base64 = String.Empty;
            _application.ActiveDocument.Save();

            string docPath = _application.ActiveDocument.FullName;
            byte[] binarydata = File.ReadAllBytes(docPath);
            base64 = System.Convert.ToBase64String(binarydata, 0, binarydata.Length);
            return base64;
        }

我确实理解错误的发生是因为指定的文档仍在 Word 中打开, 我的问题是 - 是否仍然有可能在不保存到临时文件的情况下获得文档的 base64 表示形式?

我正在使用 C# .NET 4.0 和 MS Office 2010

最佳答案

您是正确的 - Word 锁定了当前文档。为了获取当前文档字节,您需要复制现有文件 ( File.Copy ) 或保存到新文件 ( Document.SaveAsIPersistFile.Save )阅读其内容。

关于c# - 将Word文档转换为base64字符串C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11562644/

相关文章:

c# - 使用 WebDAV 和 C# 读取电子邮件的主题和正文

c# - 从 C# 转换为 C++

c# - .NET 4 中的 URL 重写?

c# - .NET 未处理的异常对话框未出现

c# - ASP :RequiredFieldValidator with the contents being a table row

c# - 删除 Doc 文件并保留 Docx 文件

C# 创建新 T()

php - 使用 php 或 ruby​​ 在 linux 中操作 word 文档(doc、docx 等)

python - 如何使用python在word文档的特定位置插入图像?

PHPWord 损坏的文件?