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

查看:146
本文介绍了在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天全站免登陆