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

前端 未结 5 2035
眼角桃花
眼角桃花 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-10 14:14

    Case-insensitive comparison is the best you can get. Extract it to a helper class just in case mankind comes up with a better method.

    public static class DirectoryInfoExtensions
    {
        public static bool IsEqualTo(this DirectoryInfo left, DirectoryInfo right)
        {
            return left.FullName.ToUpperInvariant() == right.FullName.ToUpperInvariant();
        }
    }
    

    and use:

    if (di1.IsEqualTo(di2))
    {
        // Code here
    }
    

提交回复
热议问题