How to check whether 2 DirectoryInfo objects are pointing to the same directory?

前端 未结 5 2030
眼角桃花
眼角桃花 2020-12-10 14:11

I have 2 DirectoryInfo objects and want to check if they are pointing to the same directory. Besides comparing their Fullname, are there any other better ways

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-10 14:33

    Inspired from here:

    static public bool SameDirectory(string path1, string path2)
    {
        return (
            0 == String.Compare(
                System.IO.Path.GetFullPath(path1).TrimEnd('\\'),
                System.IO.Path.GetFullPath(path2).TrimEnd('\\'),
                StringComparison.InvariantCultureIgnoreCase))
            ;
    }    
    

    Works for file too...

    (BTW theoretically questions are duplicate, but this is the original and the other one is the most answered one...)

    HTH

提交回复
热议问题