Page 1 of 1

c# DirectoryInfo

Posted: 22 Jul 2010, 09:19
by geraldhum
Hi All

I am trying to get a list of all directoriesd within a directory.

Code: Select all

DirectoryInfo di = new DirectoryInfo("\\192.168.0.1\\sharejosam");
If I change \\192.168.0.1\\sharejosam to c:\\Programs\\ it all works perfectly

But, with \\192.168.0.1\\sharejosam i get an error wherr the error states cant find directoy

c:\\192.168.0.1\\sharejosam.

why is it trying to display the folders in c: when i want it to display the networked folders?

Regards
Gerald Humphreys

Re: c# DirectoryInfo

Posted: 22 Jul 2010, 09:35
by geraldhum
Instead of having // before 192.168.0.1 i had to have //// so this is the correct way.

Code: Select all

DirectoryInfo di = new DirectoryInfo("\\\\192.168.0.1\\sharejosam");
Regards
Gerald Humphreys

Re: c# DirectoryInfo

Posted: 22 Jul 2010, 09:38
by SykomantiS
Try \\\\192.168.0.1

Wild guess. I may be completely off my rocker :P

Edit. Nevermind. You beat me to it.

Re: c# DirectoryInfo

Posted: 22 Jul 2010, 09:55
by Ron2K
It's because the backslash specifies the beginning of an escape sequence (such as \r, \n, \0, etc). Therefore, if you want a backslash character, it must itself be escaped.

So, this will work:

Code: Select all

DirectoryInfo di = new DirectoryInfo("\\\\YOUR-SERVER\\your-folder\\");
Or, even better in this scenario - use a verbatim string literal:

Code: Select all

DirectoryInfo di = new DirectoryInfo(@"\\YOUR-SERVER\your-folder\");

Re: c# DirectoryInfo

Posted: 29 Jul 2010, 07:23
by geraldhum
Ron2K wrote:
Or, even better in this scenario - use a verbatim string literal:

Code: Select all

DirectoryInfo di = new DirectoryInfo(@"\\YOUR-SERVER\your-folder\");
Thanks man,

Sorry it is a late reply, been busy.

What is the difference between my path and your path other than it is better to use the server name instead of the IP?

What does the @ mean?

Re: c# DirectoryInfo

Posted: 29 Jul 2010, 07:32
by Ron2K
It's really personal preference as to whether you use the host name or an IP address. I generally prefer host names, as they don't tend to change, plus I previously worked at a company where our network admin was overly fond of changing IP addresses at inconvenient times. :P But I think that's true anywhere; a host name is far less likely to change than an IP address, which would theoretically mean less maintenance.

Oh, and I think you need to read up on what a verbatim string literal is. ;)