IMBack

Is there away to check a valid DOS path in c#

ex: if the user types

zzz:\test.txt and the path is invalid, return error message.

 

using VS2003 .NET1.1

 

Thank you,



Re: Visual C# General check valid path

Mark Dawson

You can try System.IO.File.Exists.

Mark.






Re: Visual C# General check valid path

IMBack

Thanks, but it is not what I am looking for, this is used to check if the file exists or not, I want to check if the format path (directory) exists or not. so if the user types an invalid path:

ex: zzz:\test.html [ invalid drive location] , I want to capture it.

Thanks,





Re: Visual C# General check valid path

Sean Hederman

So, System.IO.Directory.Exists then.




Re: Visual C# General check valid path

IMBack

what about if u try zzz:\test.html [ zzz is invalid drive ] - it will give you an Exception error.





Re: Visual C# General check valid path

Sean Hederman

No, it doesn't, it returns false.




Re: Visual C# General check valid path

IMBack

Ok.

Is there away to check if the path user entered is valid (Exists) or not.

because I will never know if it is invalid path or folder does not exit using System.IO.Directory.Exists. Because if the folder does not exists I can create a folder .

Thanks,





Re: Visual C# General check valid path

Sean Hederman

If you use System.IO.Directory.Exists it will return true if the directory exists, and false if it does not. If you want to check whether the drive is valid, you can use System.IO.DriveInfo.GetDrives to give you the valid drive names.






Re: Visual C# General check valid path

IMBack

Thanks. Is there something similar in Net 1.1.





Re: Visual C# General check valid path

Sean Hederman

Yes, System.IO.Directory.GetLogicalDrives.






Re: Visual C# General check valid path

RizwanSharp

using File.Exists to check path validiy is a wrong idea and should never be used. It says MSDN here:

http://msdn2.microsoft.com/en-us/library/system.io.file.exists.aspx

See Under Remarks heading.

You can use System.Text.RegularExpression.Regex class to do this but for that you need a Regex string which i belive can be easily found through google. Try something like this File path Regex, Path Regex etc.

You'll need to write code like this:

Regex fileRegex = new Regex(regexStringHere);

Match match = fileRegex.Math(filePathHere);

if(match.Sucess)

{

// File path is valid.

}

Now its upto you how you find valid Regex for DOS compliant Paths...

I hope this will give you some right direction.

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General check valid path

IMBack

Thank you, I will check it out.

I found something, would like to share.

another way can be checked if the path is valid or not, using try and catch:

try{}

catch(COMException cex){

Response.Write(cex.ErrorCode);

if( cex.ErrorCode == -2147166139 )

{ //invalid path format.}

}





Re: Visual C# General check valid path

Mark Dawson

Interesting, I see the documentation says:

"The Exists method should not be used for path validation, this method merely checks if the file specified in path exists. Passing an invalid path to Existsl returns false."

It would be nice if they explained why this is not considered good practise.

Mark.