check valid path

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,



Answer this question

check valid path

  • SnowyT

    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.



  • wsalomon

    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,


  • da Vinci

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


  • QuantumMischief

    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.}

    }


  • netpicker9

    No, it doesn't, it returns false.

  • GoDaddy

    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,


  • NoEgo

    Yes, System.IO.Directory.GetLogicalDrives.



  • Madhu_TN

    Thanks. Is there something similar in Net 1.1.


  • etcheverrjc

    So, System.IO.Directory.Exists then.

  • MigiTheGuru

    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.



  • DBLearner

    You can try System.IO.File.Exists.

    Mark.



  • kcchesnut

    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



  • check valid path