Online or local path?

Hi there!,
what is the best way to check if a path refeers to a file on your pc or if it is online?


I already made:
if(Directory.Exists(zipPath.Substring(0, zipPath.LastIndexOfAny(new char[]{'\\','/'}))))

because Directory.Exists only works for local files (i gess), but I think this is not very clean.
Any better ideas?
LVL 10
dkloeckAsked:
Who is Participating?
 
elticConnect With a Mentor Commented:
Try using the Uri-class:

Uri u = new Uri(@"c:\test.txt");
if (u.IsLoopback)
{
     Console.WriteLine("File is local file");
}
else
{
     Console.WriteLine("File is remote file");
}
0
 
anyoneisCommented:
Directory.Exists will access network files - so that won't work reliably for you. Here is a nice little routine: http://vbnet.mvps.org/index.html?code/fileapi/pathislocalpath.htm

David
0
 
dkloeckAuthor Commented:
hmmm.. don't work after all, it always gives "File is remote file" back
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
dkloeckAuthor Commented:
btw, my way works fine. From msdn:"The Exists method does not perform network authentication. If you query an existing network share without being pre-authenticated, the Exists method will return false. "
0
 
elticCommented:
Give us your test-path, which should be local.
0
 
dkloeckAuthor Commented:
local would be everything on your pc like "c:\\test.txt" "\\asdas.bmp" "j:\\test\\test2\\asdfg.txt\\" or whatever on your pc
remote is everything else
0
 
elticCommented:
That's clear, but what is the EXACT path you've tested, which is returned as remote file, although it is locally ?
0
 
dkloeckAuthor Commented:
hmm.. i gess it was "f:\\Software\\Visual Studio Projects\\Worldwind\\Shapefiles\\AOI.zip"
0
 
dkloeckAuthor Commented:
f is one of my hard discs by the way
0
 
elticCommented:
What version of the .NET Framework do you use ?
I tried the path above with 2.0 and I get "File is local file".
0
 
dkloeckAuthor Commented:
I use VS 2003
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.