tags:

views:

195

answers:

4

Hi,

I am writing some code to add file attachments into an application I am building.

I have add & Remove working but I don't know where to start to implement open.

I have an array of bytes (from a table field) and I don't know how to make it automatically open e.g.

If I have an array of bytes which is a PDF, how do I get my app to automatically open Acrobat or whatever the currently assigned application for the extension is using C#?

+8  A: 

In order to open it in any external application, you'll need to write the bytes to disk, then use Process.Start to launch the associated application on the temporary file. Just pass the temporary filename (with the appropriate extension) as the only argument the Process.Start, and it will open that file in the appropriate application.

Some applications may have a way to feed a stream of bytes, but this would need to be handled by the target application explicitly.


For sample code, you could do something like:

byte[] filedata = GetMyByteArray();
string extension = GetTheExtension(); // "pdf", etc

string filename =System.IO.Path.GetTempFileName() + "." + extension; // Makes something like "C:\Temp\blah.tmp.pdf"

File.WriteAllBytes(filename, filedata);

var process = Process.Start(filename);
// Clean up our temporary file...
process.Exited += (s,e) => System.IO.File.Delete(filename); 
Reed Copsey
In addition to what Reed said, it's possible to provide the user with an Open With list the way the Window's desktop does. See here: http://stackoverflow.com/questions/1002270/how-does-the-os-know-what-to-do-with-a-file-when-the-open-with-option-is-sel
Scott J
This one works a treat Reed except your have forgotten to write out the bytes! :-)
Ace Grace
@Ace: Thanks - fixed.
Reed Copsey
+3  A: 

This may help a bit

        byte[] bytes = File.ReadAllBytes(@"C:\temp\file.pdf");

        string outpath = @"c:\temp\openme.pdf";
        File.WriteAllBytes(outpath, bytes);
        Process.Start(outpath);

Simply writes the byte[] to disk and then runs it with the associated application.

Mikael
Should use Path.GetTempPath or Path.GetTempFileName, not assume C:\Temp.
Reed Copsey
I was assuming he would replace that with whatever path he should use, the question was how to open the document. It's not a sure thing that it is a temp path at all, the path may come from a setting or a save dialog etc.But good point :)
Mikael
A: 

Write the data to a temporary file and open it using Process. This will use the standard programm configured for the filetype. (eg txt > notepad)

        byte[] b = new byte[]{0x0};
        var fileName = "c:\\test.txt";
        System.IO.File.WriteAllBytes(fileName, b);
        System.Diagnostics.Process.Start(fileName);
rdkleine
A: 
// get the PDF in byte form from the system
var bytes = GetFileBytes("Some identifier");
// get a valid temporary file name and change the extension to PDF
var tempFileName = Path.ChangeExtension(Path.GetTempFileName(), "PDF");
// write the bytes of the PDF to the temp file
File.WriteAllBytes(tempFileName, bytes);
// Ask the system to handle opening of this file
Process.Start(tempFileName);
Will