c# DirectoryInfo

Get help on programming - C++, Java, Delphi, etc.
Post Reply
geraldhum
Registered User
Posts: 199
Joined: 13 Feb 2009, 12:59

c# DirectoryInfo

Post 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
geraldhum
Registered User
Posts: 199
Joined: 13 Feb 2009, 12:59

Re: c# DirectoryInfo

Post 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
SykomantiS
Registered User
Posts: 14085
Joined: 06 Oct 2004, 02:00
Location: Location, Location...
Contact:

Re: c# DirectoryInfo

Post by SykomantiS »

Try \\\\192.168.0.1

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

Edit. Nevermind. You beat me to it.
User avatar
Ron2K
Forum Technical Administrator
Posts: 9050
Joined: 04 Jul 2006, 16:45
Location: Upper Hutt, New Zealand
Contact:

Re: c# DirectoryInfo

Post 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\");
Kia kaha, Kia māia, Kia manawanui.
geraldhum
Registered User
Posts: 199
Joined: 13 Feb 2009, 12:59

Re: c# DirectoryInfo

Post 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?
User avatar
Ron2K
Forum Technical Administrator
Posts: 9050
Joined: 04 Jul 2006, 16:45
Location: Upper Hutt, New Zealand
Contact:

Re: c# DirectoryInfo

Post 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. ;)
Kia kaha, Kia māia, Kia manawanui.
Post Reply