在 C# 中嵌入 Word 文档

发布于 2024-10-06 03:13:25 字数 246 浏览 1 评论 0原文

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

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

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

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

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");

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不喜欢何必死缠烂打 2024-10-13 03:13:25

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

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

更多信息请参见此处:
如何使用 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);
     }
}

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");

More info here:
How to embed and access resources by using Visual C#

Edit: 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);
     }
}
嗳卜坏 2024-10-13 03:13:25

右键单击解决方案中要存储文件的文件夹,然后选择“添加”->“添加”。现有项目。

添加文件后,您可以将项目中文件的构建操作更改为 嵌入式资源,与资源。这可以通过转到文件 VS 内的“属性”并修改“构建操作”属性来完成。

Right click the folder where you want to store the file within the Solution and choose Add -> Existing Item.

Once you add the file you can change the Build Action of the file within your project to be an Embedded Resource, versus a Resource. This can be done by going to the Properties within VS of the file and modifying the Build Action property.

月光色 2024-10-13 03:13:25

只需将其包含到您的项目中(添加现有项目),然后从打开的菜单中选择所有文件并选择您的 Word 文档。还将文档复制到 Bin/Debug 文件夹中。如果您使用安装程序,请将该文档包含在安装程序中,它应该可以工作。

Just include it to your project (add existing item) and from the menu that opens, select all files and select your word document. Also Copy the document into your Bin/Debug folder. If you are using an installer, include the document in the installer and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文