4

In a windows forms application I have a file stored as a byte[].

When the user clicks the button I want to open the file without saving it local. Is this possible? If it is then how?

Or do I have to save the byte-array as a local file an run this file then?

Thanks, Karl

godot
  • 3,422
  • 6
  • 25
  • 42
abc
  • 2,285
  • 5
  • 29
  • 64
  • What's the file type supposed to be? – keyboardP Sep 12 '13 at 12:23
  • If you haven't saved it anywhere, it isn't a file - you've just got some data in memory. What exactly do you mean by "open" in this case? And do you have any particular reason to avoid saving it? – Jon Skeet Sep 12 '13 at 12:24
  • Do you mean you want to create a stream for that array? I.e. a [`MemoryStream`](http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx)? How did this "file" end up being a byte array in the first place? Is it a serialized object? What does "opening a file" mean for you? Open it inside your app, or use a default Windows app for that extension to open it? If it's a third party app, there's not much you can do beside save it in a temporary folder and call something like `Process.Start` on it. – vgru Sep 12 '13 at 12:26
  • @keyboardP: the file type can variate, but i got the mimetype with it. Most of the time there are pdf-files or several image-types – abc Sep 12 '13 at 12:27
  • @JonSkeet: I want to open a file, just like opening it from windows explorer, so the user can look at it, read it,... The functionality should look like the "open file"-function in the browser (and I know the file is downloaded to a temp-folder there) – abc Sep 12 '13 at 12:31
  • @Groo: The byte[] is loaded into my program from a ms dynamics crm-note, where files can be added to any record. Loading it into memory brings me my bytearray – abc Sep 12 '13 at 12:33

1 Answers1

7

If you want to open in in an application (just like Windows would if you double-clicked a file with an appropriate extension), you will have to write its contents to a file:

/// <summary>
/// Saves the contents of the <paramref name="data"/> array into a 
/// file named <paramref name="filename"/> placed in a temporary folder,
/// and runs the associated application for that file extension
/// in a separate process.
/// </summary>
/// <param name="data">The data array.</param>
/// <param name="filename">The filename.</param>
private static void OpenInAnotherApp(byte[] data, string filename)
{
    var tempFolder = System.IO.Path.GetTempPath();
    filename = System.IO.Path.Combine(tempFolder, filename);
    System.IO.File.WriteAllBytes(filename, data);
    System.Diagnostics.Process.Start(filename);
}

Usage:

var someText = "This is some text.";
var data = Encoding.ASCII.GetBytes(someText);

// open in Notepad
OpenInAnotherApp(data, "somename.txt");

Note that the file name is needed only for the extension, you should probably use a random Guid or something and append the extension based on a known mimetype.

vgru
  • 49,838
  • 16
  • 120
  • 201
  • [This answer](http://stackoverflow.com/a/3393525/69809) has mappings for various mimetypes and file extensions. Note that it's not really a bijection (`application/octet-stream` comes to my mind). – vgru Sep 12 '13 at 12:40