views:

188

answers:

4

I've got a winforms app that stores the contents of files in a database. The stored files can be of just about any type (word, excel, PDF, text, image ...) the user can select just about any type of file to load.

The user can then query the database, find a file and then open it.

I've got no problems extracting the byte array from the database, as either a stream or a byte array.

Ideally I'd be able to display the file directly from a byte array or stream; at the moment I'm saving it as a temporary file and then opening that with:

Process.Start(fileName);

How can I display the file with the associated application either from any of the byte array or stream file?

A: 

Maybe you want to research a little bit on Memory Mapped File.

deerchao
+3  A: 

In windows, your only option is to do exactly what you're doing. Outlook, Internet explorer, firefox, all do this

Rob Fonseca-Ensor
I agree, this is the only option.
Jochen
Well this seems to the the consensus, and the easiest option for me, it's already coded and running. The only downside is the leftover temporary files. I can't delete them from my app as the user may leave the launched application open longer than mine.
baralong
When you say you're saving it as a temporary file, do you mean you're saving the file into the standard windows temporary directory? I use GetTempPath (http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx) for that. That way, windows will be responsible for cleaning up the file (albeit eventually)
Rob Fonseca-Ensor
Actually I'm using Path.GetTempFileName and swapping the file type for the one I've got. So yes it's in the temporary directory.
baralong
Oddly, Path.GetTempFileName actually CREATES a file, so if you are changing the extension you might want to delete the "old" one
Rob Fonseca-Ensor
A: 

you can try to open the directory containing it, but it will be the same thing you're doing right now.. if the associated app is known by the OS, then there will be no problem..

LostMohican
A: 

If you store a filename in the DB along with the byte stream, you can determine the file type from the extension. There's two options in this case:

  1. Use the registry to determine what application to use. For more info on this, take a look at this conversation on bytes.com.
  2. P/Invoke SHGetFileInfo to determine what application to use.

NB: With both options you'll still need to write the file data to a temp file on disk in order to load it.

Personally, I'd think what you're doing is probably the easiest option, anyway (unless you'd like to provide custom viewers for certain file-types, etc)

Rich