🚩 增加相对路径功能

This commit is contained in:
liufei
2022-06-08 15:52:48 +08:00
parent 7d061abadc
commit 3995084776
8 changed files with 116 additions and 47 deletions

View File

@@ -88,22 +88,27 @@ namespace GeekDesk.Util
return appData;
}
private readonly static object _MyLock = new object();
/// <summary>
/// 保存app 数据
/// </summary>
/// <param name="appData"></param>
public static void SaveAppData(AppData appData, string filePath)
{
appData.AppConfig.SysBakTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
if (!Directory.Exists(filePath.Substring(0, filePath.LastIndexOf("\\"))))
lock (_MyLock)
{
Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf("\\")));
}
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, appData);
appData.AppConfig.SysBakTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
if (!Directory.Exists(filePath.Substring(0, filePath.LastIndexOf("\\"))))
{
Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf("\\")));
}
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, appData);
}
}
}
public static void BakAppData()
@@ -192,7 +197,11 @@ namespace GeekDesk.Util
{
iconInfo.Name_NoWrite = path;
}
iconInfo.RelativePath_NoWrite = FileUtil.MakeRelativePath(Constants.APP_DIR + "GeekDesk.exe", iconInfo.Path);
string relativePath = FileUtil.MakeRelativePath(Constants.APP_DIR + "GeekDesk.exe", iconInfo.Path);
if (!string.IsNullOrEmpty(relativePath) && !relativePath.Equals(iconInfo.Path))
{
iconInfo.RelativePath_NoWrite = relativePath;
}
return iconInfo;
}

View File

@@ -157,24 +157,32 @@ namespace GeekDesk.Util
}
}
public static String MakeRelativePath(String fromPath, String toPath)
public static string MakeRelativePath(string fromPath, string toPath)
{
if (String.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath");
if (String.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath");
//if (string.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath");
//if (string.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath");
Uri fromUri = new Uri(fromPath);
Uri toUri = new Uri(toPath);
//Uri fromUri = new Uri(fromPath);
//Uri toUri = new Uri(toPath);
if (fromUri.Scheme != toUri.Scheme) { return toPath; } // path can't be made relative.
//if (fromUri.Scheme != toUri.Scheme) { return toPath; } // path can't be made relative.
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
String relativePath = Uri.UnescapeDataString(relativeUri.ToString());
if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase))
{
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}
//Uri relativeUri = fromUri.MakeRelativeUri(toUri);
//string relativePath = Uri.UnescapeDataString(relativeUri.ToString());
//if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase))
//{
// relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
//}
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)
);
return relativePath;
}