15

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
Amitesh
  • 151
  • 1
  • 1
  • 3
  • 2
    Yes, this is possible. I had done so myself. Are you asking how to do this? If so, please post the code you currently have and explain your problems with it. – Oded Dec 01 '10 at 19:10
  • 1
    What's the concrete purpose to open the files? I mean, if they are of any kind (.pdf, .png, .exe, .doc, .bat, .wav, .ms3d, what ever), what are you planning to do after loading them files into memory and for precessing the data appropriately? – Flinsch Dec 01 '10 at 19:13
  • 1
    @Oded: processing a byte stream is possible, but executing an arbitrary byte stream in .NET isn't. If you, like me, missed the `Process.Start` part, no harm, no foul, but if you saw that and are still claiming it can be done, I'll go ahead and ask: HOW?? – Randolpho Dec 01 '10 at 19:19
  • @Randolpho - Yep, missed the `Process.Start` portion... my bad. – Oded Dec 01 '10 at 19:22

5 Answers5

16

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.

johnc
  • 39,385
  • 37
  • 101
  • 139
The Smallest
  • 5,713
  • 25
  • 38
10

MemoryStream has a constructor that takes a Byte array.

So:

var bytes = GetBytesFromDatabase(); // assuming you can do that yourself
var stream = new MemoryStream(bytes);

// use the stream just like a FileStream

That should pretty much do the trick.

Edit: Aw, crap, I totally missed the Process.Start part. I'm rewriting...

Edit 2:

You cannot do what you want to do. You must execute a process from a file. You'll have to write to disk; alternatively, the answer to this question has a very complex suggestion that might work, but would probably not be worth the effort.

Community
  • 1
  • 1
Randolpho
  • 55,384
  • 17
  • 145
  • 179
  • thanks for all your quick replies. So from what i gather is that for me to open the stored attached from db, I will need to write it to disk and them open the file using the Process.Start() method. My only issue with this was that I will have to make sure the user has write access to the path where I will place the file and also am not sure how I will detect that the user has closed the file so I can delete the file from disk. – Amitesh Dec 01 '10 at 19:55
  • To give a background on what I am trying to do is the following: – Amitesh Dec 01 '10 at 19:56
  • My winforms applicaiton allows users to log cases and with the case they can save attachments (these get stored in the SQL DB). Later on they can open the attachments by double clicking on the grid. So my question is what is the best way to open the attachment files. I would have not prefered to open by saving to disk and then openning. Since I will come how need to do the clean up as I dont want these files left on users local drives – Amitesh Dec 01 '10 at 19:59
1

MemoryMappedFile?

http://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile.aspx

dexter
  • 7,063
  • 9
  • 54
  • 71
1

My only issue with this was that I will have to make sure the user has write access to the path where I will place the file...

You should be able to guarantee that the return of Path.GetTempFileName is something to which your user has access.

...and also am not sure how I will detect that the user has closed the file so I can delete the file from disk.

If you start the process with Process.Start(...), shouldn't you be able to monitor for when the process terminates?

Shibumi
  • 1,369
  • 9
  • 24
1

If you absolutely don't want to write to disk yourself you can implement local HTTP server and serve attachemnts over HTTP (like http://localhost:3456/myrecord123/attachment1234.pdf). Also I'm not sure if you get enough benefits doing such non-trivial work. You'll open files from local security zone that is slightly better then opening from disk... and no need to write to disk yourself. And you'll likely get somewhat reasonable warning if you have .exe file as attachment.

On tracking "process done with the attachment" you more or less out of luck: only in some cases the process that started openeing the file is the one that is actually using it. I.e. Office applications are usually one-instance applications, and as result document will be open in first instance of the application, not the one you've started.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179