vikrantx
vikrantx

Reputation: 603

Extract and open PPT from resources in C#

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?

Upvotes: 0

Views: 2084

Answers (1)

Christian.K
Christian.K

Reputation: 49250

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.

Upvotes: 4

Related Questions