在 C# 中嵌入 Word 文档 [英] Embed a Word Document in C#

查看:35
本文介绍了在 C# 中嵌入 Word 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to open a MS Word document from my program. At the moment, it can find it when in designer mode but when i publish my program it can't find the file. I believe I need to embed it into my program but I don't know how to do this. This is my current code to open the document:

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

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

I think the Word document needs to be embedded into the resources of the .exe but i don't know how to to do this.

有人可以帮忙提供一些建议吗?

Can anyone help with some suggestions?

推荐答案

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

Aaron is pretty right on adding an embedded resource. Do the following to access an embedded resource:

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

更多信息在这里:如何使用 Visual C# 嵌入和访问资源

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

Now to actually run the file you will need to copy the file in some temp dir. You can use the following function to save the stream.

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# 中嵌入 Word 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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