System.IO.File.Exists - what if network path and not C:\

R

Ronald S. Cook

I'm trying to see if a file exists on a server. I have the below code which
doesn't seem to work. Must I necessarily have a mapped drive to that the
path starts with X:\ or similar?

if (System.IO.File.Exists("\\MyServer\MyFolder\MySubFolder\MyFile.txt") ==
true)
{
//Do stuff...
}


Thanks,
Ron
 
P

PS

Ronald S. Cook said:
I'm trying to see if a file exists on a server. I have the below code
which doesn't seem to work. Must I necessarily have a mapped drive to
that the path starts with X:\ or similar?

if (System.IO.File.Exists("\\MyServer\MyFolder\MySubFolder\MyFile.txt") ==
true)
{
//Do stuff...
}

That code won't compile - you need an @ before the quotes.

Also the == true is not necessary as the Exists method returns true/false
itself
 
R

Ronald S. Cook

That doesn't seem to do it either.


PS said:
That code won't compile - you need an @ before the quotes.

Also the == true is not necessary as the Exists method returns true/false
itself
 
M

Mythran

Ronald S. Cook said:
That doesn't seem to do it either.

try...

if (System.IO.File.Exists(@"\\MyServer\MyFolder\MySubFolder\MyFile.txt")) {
// Do Stuff...
}

If this doesn't exist, the user of the process executing this code may not
have read rights for the specified file or....the file may not exist in that
location.

HTH,
Mythran
 
R

Ronald S. Cook

Ahh.. I think that may be it. -thx

Mythran said:
try...

if (System.IO.File.Exists(@"\\MyServer\MyFolder\MySubFolder\MyFile.txt"))
{
// Do Stuff...
}

If this doesn't exist, the user of the process executing this code may not
have read rights for the specified file or....the file may not exist in
that location.

HTH,
Mythran
 

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