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

Den*_*els 7 c# vsto ms-word .net-4.0

我正在尝试获取活动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;
        }
Run Code Online (Sandbox Code Playgroud)

我确实理解错误发生是因为指定的文档仍在Word中打开,我的问题是 - 是否仍然可以获得文档的base64表示而不需要保存到临时文件?

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

Sli*_*SFT 6

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