Take the 2-minute tour ×

I am using a .NET library function that uploads files to a server, and takes as a parameter a path to a file. The data I want to send is small and constructed at runtime. I can save it to a temporary file and then upload it.

Since my application will be deployed in a variety of environments, and I don't know if I'll be able to create the temporary file reliably, it would be preferable to be able to pass in a path to a virtual file in memory.

I can't change the library; I know it executes the following on the file:

LibraryUploadFunction(string filename) {
    fileName = Path.GetFullPath(fileName);

Is it possible to avoid writing the file to disk?



The library call is Webclient.UploadFile, as pointed out in the answers, there are many workarounds possible, including using alternative libraries, of which there are many.

share|improve this question
Which library is it? –  JDB May 3 '13 at 13:28

3 Answers 3

up vote 9 down vote accepted

No, your library is fundamentally inflexible in this aspect, by the fact that it uses FileStream.


  • Use a ramdrive and specify a path on that, in order to avoid actually hitting disk
  • Depending on where the library comes from, either request a change (if it's closed source) or just make the change if it's open source
  • Use a different library. (What's special about this one?)
share|improve this answer

If the library exposes another method accepting a Stream, you could use a MemoryStream.

If it accepts a SafeFileHandle, you could use a MemoryMappedFile.

Otherwise, you'll have to be satisfied with a file on disk.

share|improve this answer

If I understand you correctly, you want to use one of the other WebClient.Upload* methods. For example, if you have your data in a byte[], use UploadData.

Another option, if you want to upload the data as a stream is to use OpenWrite.

share|improve this answer
I did try UploadString, and now I've tried UploadData. Both caused the server I was communicating with to reboot :-(. I could dig into the format of the POST that WebClient makes to figure out exactly where the problem is, but that would defeat the purpose of using a simple http client like WebClient. Temporary files it is! –  Stefan May 7 '13 at 8:25
@Stefan It caused your server to reboot? Then there is something very wrong with your server and you should figure out what it is. –  svick May 7 '13 at 12:28
Hehe...it's not "my" server as such. It's a network connected embedded device, which is out of my control. There are thousands of these things in the field. So pretty much I'm stuck with sending requests in a format it likes. At least I know of one way to get data to it successfully, so I can live with that! –  Stefan May 7 '13 at 16:34

Your Answer


