Second path fragment must not be a drive or UNC name - Create Subdirectory Error - C#

Question

Tuesday, September 3, 2013 4:50 PM


I have an exception in the third line ofthis code "Second path fragment must not be a drive or UNC name"

DirectoryInfo labdi = new DirectoryInfo(Back.mainfolderpath + @"\news\l");
DirectoryInfo tld = new DirectoryInfo(labdi.FullName + @"\" + NorA.sn.labl[i]);
tld = labdi.CreateSubdirectory(labdi.FullName + @"\" + NorA.sn.labl[i] + @"\");

There is no useful way on the web. Thank You.:!

All replies (5)

Tuesday, September 3, 2013 7:11 PM âś…Answered

Just to complete my answer you should be using either 

Directory.CreateDirectory with the full path like in your example di.FullName+@"\saber"

or

di.CreateSubdirectory("saber");


Tuesday, September 3, 2013 5:02 PM

My guess is that NorA.sn.labl[I] is empty so you get this as a path:  <blah>\

That isn't a valid file path so it blows up.  There is no need to put a trailing slash so just leave it off and the second line should be fine.  Ultimately though you should consider validating the path first and verifying it's existence because CreateSubdirectory, IIRC, will blow up if you give it a nested path and one or more of the parents don't exist.  It'll also blow up if it already exists.  You should also consider using Path.Combine to build up your paths rather than manually putting slashes everywhere as it'll fail if strings have the slash already.

Michael Taylor
http://msmvps.com/blogs/p3net


Tuesday, September 3, 2013 5:40 PM

Are You Kidding me? nora.sn.lab[i] is not empty. You Should just try this and give me the result. if it was file and have no exception so give me the right way of doing it.

private void button1_Click(object sender, EventArgs e)
        {
            DirectoryInfo di = new DirectoryInfo(@"E:\Computer\Ikad\ProjectFolder\article");
            di.CreateSubdirectory(di.FullName+@"\saber");
        }

it has exception on second line if method. on di.createsubdirectory("");


Tuesday, September 3, 2013 5:45 PM

It's Fine. I should put @"saber" instead of

di.FullName+@"\saber"

Good


Tuesday, September 3, 2013 7:05 PM

DirectoryInfo.FullName will add an extra \ at the end of the path.

E:\Computer\Ikad\ProjectFolder\article\

When you append \saber you get

E:\Computer\Ikad\ProjectFolder\article**\**saber