🐛 修复了某些路径无法建立相对路径导致程序启动崩溃的bug

This commit is contained in:
liufei
2022-06-17 10:03:35 +08:00
parent 240008ce2c
commit 001d807bb3
7 changed files with 82 additions and 26 deletions

View File

@@ -159,16 +159,23 @@ namespace GeekDesk.Util
public static string MakeRelativePath(string fromPath, string toPath)
{
if (string.IsNullOrEmpty(toPath) || string.IsNullOrEmpty(fromPath)) return null;
Uri file = new Uri(@toPath);
// Must end in a slash to indicate folder
Uri folder = new Uri(@fromPath);
string relativePath =
Uri.UnescapeDataString(
folder.MakeRelativeUri(file)
.ToString()
.Replace('/', Path.DirectorySeparatorChar)
);
string relativePath = null;
try
{
if (string.IsNullOrEmpty(toPath) || string.IsNullOrEmpty(fromPath)) return null;
Uri file = new Uri(@toPath);
// Must end in a slash to indicate folder
Uri folder = new Uri(@fromPath);
relativePath =
Uri.UnescapeDataString(
folder.MakeRelativeUri(file)
.ToString()
.Replace('/', Path.DirectorySeparatorChar)
);
} catch (Exception ex)
{
LogUtil.WriteErrorLog(ex, "建立相对路径出错:fromPath:" + fromPath + ",toPath:" + toPath);
}
return relativePath;
}