locked
How can I check valid directory name RRS feed

  • Question

  • User-1355965324 posted

    Hi

    How can I find if the given name is valid for creating the directory Name for example

    \\11.11.1.111\d$\GO_Document_Management\\qwe_qwe_200\        // It is valid for directory name

    \\11.11.1.111\d$\GO_Document_Management\\qwe_<>_///_qwe_200\    // It is not valid for directory name  . I want to avoid all forbidden characters while creating the folder

    Please any method in Asp.net core to find the correct name for folder

    Kind regards

    Pol

     

    Thursday, February 20, 2020 3:17 PM

Answers

  • User475983607 posted

    polachan

    Please any method in Asp.net core to find the correct name for folder

    This falls under basic validation.  Invalid characters in Windows filenames is not hidden.  

    < (less than)
    > (greater than)
    : (colon)
    " (double quote)
    / (forward slash)
    \ (backslash)
    | (vertical bar or pipe)
    ? (question mark)
    * (asterisk)

    A regular expression is good approach for validating inputs.  As for correcting the name, that requirement is something you need to come up with and explain to the community.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 20, 2020 3:27 PM
  • User475983607 posted

    polachan

    I applied to  check valid dicrectory name using bool isForbidden  = path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0  but not working 

    I recommend reading the documentation for the framework method you are trying to use when the framework does not function as you expect!  As recommend in many of your posts, reading the docs really does helps.

    https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidpathchars?view=netframework-4.8

    The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names. The full set of invalid characters can vary by file system. For example, on Windows-based desktop platforms, invalid path characters might include ASCII/Unicode characters 1 through 31, as well as quote ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).

    static void Main(string[] args)
    {
        string path = "tes>t";
        var invlaid = System.IO.Path.GetInvalidPathChars().ToList();
        invlaid.AddRange(new List<Char>() { '<', '>', ':', '"', '/', '|', '?', '*', '\\'});
        bool isForbidden = path.IndexOfAny(invlaid.ToArray()) >= 0;
    
        Console.WriteLine(isForbidden);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 20, 2020 5:16 PM

All replies

  • User475983607 posted

    polachan

    Please any method in Asp.net core to find the correct name for folder

    This falls under basic validation.  Invalid characters in Windows filenames is not hidden.  

    < (less than)
    > (greater than)
    : (colon)
    " (double quote)
    / (forward slash)
    \ (backslash)
    | (vertical bar or pipe)
    ? (question mark)
    * (asterisk)

    A regular expression is good approach for validating inputs.  As for correcting the name, that requirement is something you need to come up with and explain to the community.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 20, 2020 3:27 PM
  • User-1355965324 posted

    I applied to  check valid dicrectory name using bool isForbidden  = path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0  but not working 

    Thursday, February 20, 2020 4:46 PM
  • User475983607 posted

    polachan

    I applied to  check valid dicrectory name using bool isForbidden  = path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0  but not working 

    I recommend reading the documentation for the framework method you are trying to use when the framework does not function as you expect!  As recommend in many of your posts, reading the docs really does helps.

    https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidpathchars?view=netframework-4.8

    The array returned from this method is not guaranteed to contain the complete set of characters that are invalid in file and directory names. The full set of invalid characters can vary by file system. For example, on Windows-based desktop platforms, invalid path characters might include ASCII/Unicode characters 1 through 31, as well as quote ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).

    static void Main(string[] args)
    {
        string path = "tes>t";
        var invlaid = System.IO.Path.GetInvalidPathChars().ToList();
        invlaid.AddRange(new List<Char>() { '<', '>', ':', '"', '/', '|', '?', '*', '\\'});
        bool isForbidden = path.IndexOfAny(invlaid.ToArray()) >= 0;
    
        Console.WriteLine(isForbidden);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 20, 2020 5:16 PM
  • User753101303 posted

    Hi,

    "doesn't work" that is ? You could create your own based on this a as a starting point and enhance it over time (or use rather a "white list" approach ? This name is a user input ?)

    Thursday, February 20, 2020 5:48 PM