5

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

6
  • What's the file type supposed to be? Commented Sep 12, 2013 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? Commented Sep 12, 2013 at 12:24
  • Do you mean you want to create a stream for that array? I.e. a MemoryStream? 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. Commented Sep 12, 2013 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 Commented Sep 12, 2013 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) Commented Sep 12, 2013 at 12:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer has mappings for various mimetypes and file extensions. Note that it's not really a bijection (application/octet-stream comes to my mind).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.