我想从我的程序中打开一个 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/