从 C# 资源中提取并打开 PPT

发布于 2025-01-06 22:15:53 字数 248 浏览 0 评论 0原文

我想在 PowerPoint 查看器中查看演示文稿,ppt 文件位于资源中。所以问题是我如何访问它并在 PowerPoint 查看器中查看。

这是示例代码

Process.Start(@"C:\Program Files\Microsoft Office\Office12\PPTVIEW.exe",**@"e:\presentation.ppt")**;

如何用资源中包含的 ppt 替换此路径?

I want to view presentation in PowerPoint viewer, ppt file is in a resources. so the problem is that how can i access it and view in PowerPoint viewer.

Here is sample code

Process.Start(@"C:\Program Files\Microsoft Office\Office12\PPTVIEW.exe",**@"e:\presentation.ppt")**;

How can i replace this path by ppt containing in resources?

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

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

发布评论

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

评论(1

剪不断理还乱 2025-01-13 22:15:53

实际上,你所要求的是一个常见的模式,这里有一些相关的问题和答案。

基本上,您通常要做的事情如下:

  1. 找到有问题的资源并打开它的资源流。
  2. 如果您的目标 API 无法直接处理流或字节数组,请将流保存到(临时)文件。
  3. 对文件或直接对流/字节数组执行任何操作(正如我所说,如果支持的话)。
  4. 最终从步骤 1 中删除临时文件(如果有)。

因此,您首先提取 PPT 文件(实际上,它是 PPT 文件并不重要,可以是任何文件或字节 blob)。

string tempFile = Path.GetTempFileName();

using (Stream input = assembly.GetManifestResourceStream("MyPresentation.PPT"))
using (Stream output = File.Create(tempFile))
{
   input.CopyTo(output); // Stream.CopyTo() is new in .NET 4.0, used for simplicity and illustration purposes.
}

然后使用 Process.Start()。您无需指定 Powerpoint 可执行文件的路径,因为 PPT 应该是通过 PowerPoint 或 PowerPoint Viewer 注册的文件扩展名。如果您已安装两者,您可能仍需要提供相关可执行文件的路径,以防止启动错误的应用程序。确保您没有对路径进行硬编码,而是尝试从注册表中检索它(或类似的,我没有检查,因为现在变得太具体了)。

using (var process = Process.Start(tempFile))
{
   process.WaitForExit();
   // remove temporary file after use
   File.Delete(tempFile);
}

注意:我遗漏了一些您可能想要在实际应用程序中添加的错误处理。

Actually, what you ask for is a common pattern and there are some related questions and answers here on SO.

Basically what you do in general is the following:

  1. locate the resource in question and open a resource stream to it.
  2. Save the stream to a (temporary) file if your target API cannot deal with streams or byte arrays directly.
  3. Perform whatever operation on the file or directly on the stream/byte array (as I said, if supported).
  4. Eventually remove the temporary file, if any, from step 1.

So, you first extract the PPT file (actually it doesn't really matter that it is a PPT file, could by any file or byte blob for that matter).

string tempFile = Path.GetTempFileName();

using (Stream input = assembly.GetManifestResourceStream("MyPresentation.PPT"))
using (Stream output = File.Create(tempFile))
{
   input.CopyTo(output); // Stream.CopyTo() is new in .NET 4.0, used for simplicity and illustration purposes.
}

Then you open it using Process.Start(). You don't need to specify the path to the Powerpoint executable, as PPT should be a registered file extension with either PowerPoint or the PowerPoint Viewer. If you have both installed, you may still want to provide the path to the relevant executable to prevent launching the wrong application. Make sure that you don't hardcode the path though, but try to retrieve it from the registry (or similar, I haven't checked because that gets too specific now).

using (var process = Process.Start(tempFile))
{
   process.WaitForExit();
   // remove temporary file after use
   File.Delete(tempFile);
}

Note: I left out quite some error handling that you might want to add in a real application.

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