c# - 在 C# 中嵌入 Word 文档

标签 c# resources ms-word embed

我想从我的程序中打开一个 MS Word 文档。目前,它可以在设计器模式下找到它,但是当我发布我的程序时它找不到该文件。我相信我需要将它嵌入到我的程序中,但我不知道该怎么做。这是我当前打开文档的代码:

System.Diagnostics.Process.Start("Manual.docx");

我认为需要将 Word 文档嵌入到 .exe 的资源中,但我不知道该怎么做。

任何人都可以提供一些建议吗?

最佳答案

Aaron 在添加嵌入式资源方面非常正确。执行以下操作以访问嵌入式资源:

Assembly thisAssembly;
thisAssembly = Assembly.GetExecutingAssembly();
Stream someStream;
someStream = thisAssembly.GetManifestResourceStream("Namespace.Resources.FilenameWithExt");

更多信息在这里: How to embed and access resources by using Visual C#

编辑:现在要实际运行文件,您需要将文件复制到某个临时目录中。您可以使用以下函数来保存流。

public void SaveStreamToFile(string fileFullPath, Stream stream)
{
    if (stream.Length == 0) return;

    // Create a FileStream object to write a stream to a file
    using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
    {
        // Fill the bytes[] array with the stream data
        byte[] bytesInStream = new byte[stream.Length];
        stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

        // Use FileStream object to write to the specified file
        fileStream.Write(bytesInStream, 0, bytesInStream.Length);
     }
}

关于c# - 在 C# 中嵌入 Word 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4367311/

相关文章:

Excel 正在等待 Word 文档完成 OLE 操作

c# - 如何更改 HttpClient 响应的编码

c# - 将 List<Point> 存储在字符串中并解析回来的最佳方法

c# - 在 C# 中为方法或构造函数提供过多的重载是一种不好的形式吗?

php - __在 PHP "Cannot use object of type stdClass as array"中获取资源

android - 如果我收到警告 Converting to string : TypedValue? 如何识别代码不正确的地方

vba - 检索形状的名称

c# - 0x800a1391 - JavaScript 运行时错误 : 'jQuery' is undefined

c# - 如何以专业可​​靠的方式以编程方式发送短信?

c# - 如何将gridview模板化的Item数据存入DB并生成word文档?