locked
check valid path RRS feed

  • Question

  • 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,

    Friday, January 12, 2007 3:54 PM

Answers

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

    }

     

    Friday, January 12, 2007 6:21 PM

All replies

  • You can try System.IO.File.Exists.

     

    Mark.

    Friday, January 12, 2007 4:02 PM
  •  

    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,

    Friday, January 12, 2007 4:13 PM
  • So, System.IO.Directory.Exists then.
    Friday, January 12, 2007 4:59 PM
  • what about if u try zzz:\test.html  [ zzz is invalid drive ] - it will give you an Exception error.

     

    Friday, January 12, 2007 5:02 PM
  • No, it doesn't, it returns false.
    Friday, January 12, 2007 5:05 PM
  • 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,

    Friday, January 12, 2007 5:13 PM
  • 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.

    Friday, January 12, 2007 5:16 PM
  • Thanks. Is there something similar in Net 1.1.

     

     

    Friday, January 12, 2007 5:23 PM
  • Yes, System.IO.Directory.GetLogicalDrives.

    Friday, January 12, 2007 5:30 PM
  • 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

    • Proposed as answer by Tamás Kozma Thursday, June 23, 2011 9:00 AM
    • Unproposed as answer by Tamás Kozma Thursday, June 23, 2011 9:00 AM
    Friday, January 12, 2007 5:45 PM
  • 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.}

    }

     

    Friday, January 12, 2007 6:21 PM
  • 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.

    Friday, January 12, 2007 6:28 PM
  • The reason is pretty clear: There is a HUGE difference between a VALID path and path that EXISTS.

    a valid path is z:\somedir\somefile.txt

    this is regardless of whether you even have a Z: drive.

    if you HAVE a Z: drive and a 'somedir' directory on it with 'somefile.txt' in the directory, then this valid path exists.

    but if you don't, this path is still valid, because it conforms to the format of what a path should look like. in this case the path is valid, but not for use on your computer.

     

    for example: you have an xml file that contains some configuration settings and you want to make sure the config file is valid.

    Code Snippet

    <Settings>

    <log>x:\logdir\myapp\syslog.log</log>

    </Settings>

     

    in the above example, YOU may not have an X: drive, but that doesn't mean that the above entry is invalid.

     

    an invalid entry would be:

    Code Snippet

    <Settings>

    <log>C:@#$%A/w00t\syslog.log</log>

    </Settings>

     

     

    regardless of the system you use, the above path is invalid.
    Tuesday, April 1, 2008 9:53 PM