alielie
2026-09-10 9ec80cafc23f5ee3278cacce3c9f86f4087b435a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.ComponentModel;
using System.IO;
using System.Numerics;
using System.Runtime.CompilerServices;
 
namespace OpenTap.Addin.Util
{
    public class LocationString : INotifyPropertyChanged
    {
        #region ...
        public event PropertyChangedEventHandler PropertyChanged;
 
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            UserInput.NotifyChanged(this, propertyName);
        }
 
        protected void Set([CallerMemberName] string name = null)
        {
            OnPropertyChanged(name);
        }
 
        protected void Set<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
        {
            old = @new;
            if (propertyName != null)
            {
                Set(propertyName);
            }
        }
        #endregion
 
        private string relativeLocation = string.Empty;
        public string RelativeLocation
        {
            get => relativeLocation;
            set
            {
                Set(ref relativeLocation, value);
            }
        }
 
        private string absoluteLocation = string.Empty;
        public string AbsoluteLocation
        {
            get => absoluteLocation;
            set
            {
                Set(ref absoluteLocation, value);
            }
        }
 
        private static string AppendDirectorySeparator(string path)
        {
            // 如果是目录路径,确保以分隔符结尾
            if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()) &&
                !path.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
            {
                // 检查是否是目录(简单判断:没有扩展名或明确是目录)
                if (Directory.Exists(path))
                    path += Path.DirectorySeparatorChar;
            }
            return path;
        }
 
        public static string GetRelativePath(string fromPath, string toPath)
        {
            // 获取绝对路径
            fromPath = Path.GetFullPath(fromPath);
            toPath = Path.GetFullPath(toPath);
 
            // 如果路径不在同一根目录下(如 C:\ 和 D:\),无法计算相对路径
            if (!string.Equals(Path.GetPathRoot(fromPath),
                              Path.GetPathRoot(toPath),
                              StringComparison.OrdinalIgnoreCase))
            {
                return toPath; // 返回绝对路径
            }
 
            // 使用 Uri 计算相对路径
            Uri fromUri = new Uri(AppendDirectorySeparator(fromPath));
            Uri toUri = new Uri(AppendDirectorySeparator(toPath));
 
            Uri relativeUri = fromUri.MakeRelativeUri(toUri);
            string relativePath = Uri.UnescapeDataString(relativeUri.ToString());
 
            // 转换为文件路径格式
            relativePath = relativePath.Replace('/', Path.DirectorySeparatorChar);
 
            // 移除开头的分隔符(如果有)
            if (relativePath.StartsWith(Path.DirectorySeparatorChar.ToString()))
                relativePath = relativePath.Substring(1);
 
            return relativePath;
        }
 
        public static string GetAbsolutePath(string basePath, string relativePath)
        {
            if (string.IsNullOrEmpty(basePath))
            {
                return relativePath;
            }
            string dirPath = Path.GetDirectoryName(basePath);
            string combinedPath = Path.Combine(dirPath, relativePath);
            return Path.GetFullPath(combinedPath);
        }
 
        public void Refresh(string basePath)
        {
            if (string.IsNullOrEmpty(basePath))
            {
                return;
            }
 
            string path = LocationString.GetAbsolutePath(basePath, RelativeLocation);
            if (File.Exists(path))
            {
                if (Path.IsPathRooted(RelativeLocation))
                {
                    RelativeLocation = LocationString.GetRelativePath(basePath, path);
                }
                AbsoluteLocation = path;
                return;
            }
            if (File.Exists(AbsoluteLocation))
            {
                path = LocationString.GetRelativePath(basePath, AbsoluteLocation);
                RelativeLocation = path;
                return;
            }
        }
 
        public bool GetLocation(string basePath, out string location)
        {
            location = null;
            if (Path.IsPathRooted(RelativeLocation))
            {
                if (File.Exists(RelativeLocation))
                {
                    location = RelativeLocation;
                    return true;
                }
            }
            else
            {
                string path = GetAbsolutePath(basePath, RelativeLocation);
                if (File.Exists(path))
                {
                    location = path;
                    return true;
                }
            }
 
            if (File.Exists(AbsoluteLocation))
            {
                location = AbsoluteLocation;
                return true;
            }
            return false;
        }
    }
}