Save byte array to UNC path

When i use

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

It seems that it always adds “C:” to the beginning, so it tries to save c: \ server \ temp ...

Is there any way around this?

+5
source share
3 answers

I believe this is due to the fact that the double backslash is not reset.

Try this instead:

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

Your current path is evaluated as \server\tmp\...which by default will be c:\server\tmp\....

To create a UNC path, you will need an additional escaped-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);
+3
source

How about him:

System.IO.File.WriteAllBytes(Path.Combine(@"\\server\tmp", FileName), fileData);
+1
source

All Articles