Directory.CreateDirectory problem

G

Guest

Hi,

I am having a bit of a nightmare with the following:

if(!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);

When I add a watch to this while debugging I see that the value of
folderPath is

@"\\MachineName\Foldername\AnotherFolderName\0\001\487"

However, this throws up a DirectoryNotFoundException with the following
message:

"Could not find a part of the path \"\\\\MachineName\\Foldername\"."

I clicked Start--> Run and entered \\MachineName\Foldername, which took me
to the directory. So why can't it be found in code? I can navigate all the
way down to \\MachineName\Foldername\AnotherFolderName\0\001 (it is the last
folder, 487, that I am trying to create). If I am able to navigate to the
folder through windows explorer, shouldn't I be able to access it in code?
This has worked previously so that adds even more to the confusion.

I assume that the \\\\ in the error message rather than just \\ is because
of the escape characters, but apart from that I cannot think what the problem
may be.

Any help would be greatly appreciated.
 
R

rossum

Hi,

I am having a bit of a nightmare with the following:

if(!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);

When I add a watch to this while debugging I see that the value of
folderPath is

@"\\MachineName\Foldername\AnotherFolderName\0\001\487"
You are escaping the string with @, do you really need the "\\" at the
start?

Have you tried:

@"\MachineName\Foldername\AnotherFolderName\0\001\487"

alternatively, Windows can recognise '/' as a separator, try:

@"/MachineName/Foldername/AnotherFolderName/0/001/487"


rossum
 
D

Doug Semler

cashdeskmac said:
Hi,

I am having a bit of a nightmare with the following:

if(!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);

When I add a watch to this while debugging I see that the value of
folderPath is

@"\\MachineName\Foldername\AnotherFolderName\0\001\487"

However, this throws up a DirectoryNotFoundException with the following
message:

"Could not find a part of the path \"\\\\MachineName\\Foldername\"."

I clicked Start--> Run and entered \\MachineName\Foldername, which took me
to the directory. So why can't it be found in code? I can navigate all
the
way down to \\MachineName\Foldername\AnotherFolderName\0\001 (it is the
last
folder, 487, that I am trying to create). If I am able to navigate to the
folder through windows explorer, shouldn't I be able to access it in code?
This has worked previously so that adds even more to the confusion.

I assume that the \\\\ in the error message rather than just \\ is because
of the escape characters, but apart from that I cannot think what the
problem
may be.

Any help would be greatly appreciated.

Reading the documentation, Directory.CreateDirectory requires a mapped
drive:

DirectoryNotFoundException | The specified path is invalid (for example, it
is on an unmapped drive).

IOW, you cannot use it to create a directory on a network path.



--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
D

Doug Semler

rossum said:
You are escaping the string with @, do you really need the "\\" at the
start?

Have you tried:

@"\MachineName\Foldername\AnotherFolderName\0\001\487"

alternatively, Windows can recognise '/' as a separator, try:

@"/MachineName/Foldername/AnotherFolderName/0/001/487"


Yes, because he is accessing a UNC path...

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
G

Guest

Doug Semler said:
Reading the documentation, Directory.CreateDirectory requires a mapped
drive:

DirectoryNotFoundException | The specified path is invalid (for example, it
is on an unmapped drive).

IOW, you cannot use it to create a directory on a network path.

As I mentioned in my original post, this functionality was working
previously. It was tested in 4 different departments and all was fine.
However, I will look into this further....

Thanks for the replies so far.
 
G

Guest

Fixed the problem. Doug was right, in that the network drive was not mapped
(some new paths had been added to the DB but the machines were not yet
online).

Thanks for the help, everyone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top