Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file from byte array

Tags:

c#

.net

file

I am storing attachments in my applications.

These gets stored in SQL as varbinary types.

I then read them into byte[] object.

I now need to open these files but dont want to first write the files to disk and then open using Process.Start().

I would like to open using inmemory streams. Is there a way to to this in .net. Please note these files can be of any type

like image 944
Amitesh Avatar asked Dec 01 '10 19:12

Amitesh


People also ask

How to convert byte array to file in c#?

Using File. The static File class in the System.IO namespace provides a simple static method WriteAllBytes() that we can use to write all the data from a byte array to a file. => File. WriteAllBytes(filePath, data); public static void SaveByteArrayToFileWithStaticMethod(byte[] data, string filePath) => File.

How do I write a byte array to a file?

In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementation: Convert a String into a byte array and write it in a file.


Video Answer


1 Answers

You can write all bytes to file without using Streams:

System.IO.File.WriteAllBytes(path, bytes);

And then just use

Process.Start(path);

Trying to open file from memory isn't worth the result. Really, you don't want to do it.

like image 146
The Smallest Avatar answered Oct 13 '22 04:10

The Smallest