See if file path is inside a directory

前端 未结 2 922
后悔当初
后悔当初 2021-01-23 20:51

How can I check whether a path to a file that doesn\'t necessarily exists points to a location inside a particular directory? Say I have a method:

bool IsInside(         


        
2条回答
  •  -上瘾入骨i
    2021-01-23 21:15

    You could try the string.IndexOf method. If you use the overload with the StringComparison enumeration it should give you the result you need.

    From above link:

    Reports the zero-based index of the first occurrence of one or more characters, or the first occurrence of a string, within this string. The method returns -1 if the character or string is not found in this instance.

    bool IsInside(string folder, string path)
    {
       if (path.IndexOf(folder,StringComparison.OrdinalIgnoreCase) != -1)
            return true;
        else
            return false;    
    

提交回复
热议问题