在C#中嵌入Word文档

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

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

我认为Word文档需要嵌入到.exe的资源中,但我不知道如何做到这一点。

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

Aaron非常适合添加嵌入式资源。 执行以下操作以访问嵌入式资源:

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

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

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

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