Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Byte Array to UNC Path

Tags:

c#

When I use

System.IO.File.WriteAllBytes("\\server\\tmp\\" + FileName, fileData);

It always seems to add "C:" to the beginning so it tries to save to c:\server\temp...

Is there a way around this?

like image 428
griegs Avatar asked Apr 26 '13 02:04

griegs


People also ask

How do I convert a byte array to a file?

Let’s consider a few different ways to convert a byte array to a file. To download the source code for this article, you can visit our GitHub repository. The BinaryWriter is simply a class that helps to store strings of different encodings as well as raw binary data into files or memory locations.

How do I save a byte[] to a file?

This article shows a few ways to save a byte [] into a file. For JDK 1.7 and above, the NIO Files.write is the simplest solution to save byte [] to a file. // bytes = byte [] Path path = Paths.get ( "/path/file" ); Files.write (path, bytes); FileOutputStream is the best alternative.

How to save a byte array using binarywriter in Java?

Let’s write a method to save a byte array using the BinaryWriter class: We pass the FileStream class into the BinaryWriter object and then call on the BinaryWriter to help us write the data into the stream. The FileStream can manipulate data in a class itself.


2 Answers

I believe this is because the double backslash isn't escaped.

Try this instead:

System.IO.File.WriteAllBytes(@"\\server\tmp\" + FileName, fileData);
like image 99
Srikanth Venugopalan Avatar answered Sep 28 '22 08:09

Srikanth Venugopalan


Your current path evaluates to \server\tmp\... which will default to c:\server\tmp\....

To make a UNC path, you'll need an extra escaped directory-separator:

System.IO.File.WriteAllBytes("\\\\server\\tmp\\" + FileName, fileData);

or you can use a string-literal instead:

System.IO.File.WriteAllBytes(@"\\server\tmp\" + FileName, fileData);
like image 41
newfurniturey Avatar answered Sep 28 '22 09:09

newfurniturey