FileStream writes to C drive instead of UNC path given

  • Thread starter Thread starter Don Gollahon
  • Start date Start date
D

Don Gollahon

The following writes to my C drive instead of the server\path given:

fstream = new FileStream(vpath+myFileName, FileMode.CreateNew);
fstream.Write(myFileBytes, 0, myFileSize);
fstream.Close();

Where vpath = "\\MyServer\MyDir\";

And myFileName is an XML file name.

The file ends up in "C:\MyServer\MyDir\myFileName.xml".

Why is it doing this? Yes, I have writes to the server. I can copy to
it using windows explorer.

..NET 1.1, C#.
 
Don,
Where vpath = "\\MyServer\MyDir\";

Is that your actual asseignment code? It shouldn't even compile. Keep
in mind that \ is an escape character is C#. You need

vpath = "\\\\MyServer\\MyDir\\";

or

vpath = @"\\MyServer\MyDir\";


Mattias
 
Mattias said:
Don,


Is that your actual asseignment code? It shouldn't even compile. Keep
in mind that \ is an escape character is C#. You need

vpath = "\\\\MyServer\\MyDir\\";

or

vpath = @"\\MyServer\MyDir\";


Mattias

vpath is set from parameters in a config file. "\\MyServer\MyDir\" is
what is in the config file. When I step thru the code and view vpath
while running it appears as "\\MyServer\MyDir\", too.
 
Problem solved:

The config file accidently had a space in front of the file path so it
was actually " \\myServer\myDir\". I removed the space and it works
fine.

Could someone tell me why FileStream did not give an invalid path
error? And why did it convert the path to "C:\myServer\myDir\"?

Is this fixed in a later version of .NET?

Thanks.
 
Could someone tell me why FileStream did not give an invalid path
error? And why did it convert the path to "C:\myServer\myDir\"?

Since the path was not a valid UNC path, the path was instead interpreted
as a "relative path". A "relative path" is a valid path.
Is this fixed in a later version of .NET?

It does not need to be fixed. The method gave you the expected result per
the written MSDN documentation.
 
Michael said:
Since the path was not a valid UNC path, the path was instead
interpreted as a "relative path". A "relative path" is a valid path.


It does not need to be fixed. The method gave you the expected
result per the written MSDN documentation.

Whenever two backslashes are together in a relative path it is an
error. This should not have been converted from " \\myserver\mydir\"
to "c:\myserver\mydir\". I would rather it just tell me there was in
invalid path. I've seen other compilers do this, too, now that I think
about it. Like Delphi for Win32 where I have accidently included "\\"
instead of just "\" somewhere in the path. But it invariably still
raises problems later on so I just wish it would throw an appropriate
exception.
 
Back
Top