none
How to check if a file path of the my system is added as string is well formed RRS feed

  • Question

  • I need to check if a path set is as a string is correct:

    Example : C:\Intel\Logs it should return true.

    If C:\\Intel\Logs it should return false

    I tried with the below code

    bool isiValid = Uri.IswellFormedUri(@"C:\Intel\Logs",UriKind.RelativeOrAbsolute);

    but always I am getting isValid as false.



    • Edited by Joy123456 Wednesday, February 6, 2019 1:33 PM
    Wednesday, February 6, 2019 1:19 PM

All replies

  • Depends upon what you mean by valid. A valid path doesn't mean it actually exists. To validate a path you could use Uri but you probably want to handle only files, not any URL so you'll want to add the scheme. Maybe something like this.

    class Program
    {
        static void Main ( string[] args )
        {
            var paths = new[]
            {
                @"C:\Test",
                @":a|b",
                @"\Temp",
                @"..\Parent"
            };
    
            foreach (var path in paths)
                Console.WriteLine("{0} is {1}", path, IsValidPath(path) ? "valid" : "invalid");
        }
    
        static bool IsValidPath ( string path )
        {
            return Uri.TryCreate(Uri.UriSchemeFile + path, UriKind.RelativeOrAbsolute, out var uri);
        }
    }
    If you want to verify that a path exists however you can use Directory.Exists


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, February 6, 2019 3:13 PM
    Moderator
  • Hi Joy123456,

    Thank you for posting here.

    According to your code, the false is caused by the wrong format uri string @"C:\Intel\Logs".

    bool isiValid = Uri.IswellFormedUri(@"C:\Intel\Logs",UriKind.RelativeOrAbsolute);

    You should use Uri.IsWellFormedUriString(String, UriKind) method. The uristring parameter need explicit file path. The implicit file paths are not compliant with the URI specification and so should be avoided when possible.

    For example:

    Uri uri1 = new Uri("C:/test/path/file.txt") // Implicit file path.
    Uri uri2 = new Uri("file:///C:/test/path/file.txt") // Explicit file path.

    For more details about the URI specification, please refer to the MSDN document.

    https://docs.microsoft.com/en-us/dotnet/api/system.uri?view=netframework-4.7.2

    Please try the code below, it would return true.

        bool isiValid = Uri.IsWellFormedUriString("file:///C:/Intel/Logs", UriKind.RelativeOrAbsolute);

    Result:

    And for Uri.IsWellFormedUriString(String, UriKind) method, when the requirements is not satisfied, it would return false.

    For more details about the requirements, you could refer tothe MSDN document.

    https://docs.microsoft.com/en-us/dotnet/api/system.uri.iswellformeduristring?view=netframework-4.7.2

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Tuesday, February 12, 2019 6:25 AM
    Thursday, February 7, 2019 2:54 AM
    Moderator