🚩 增加相对路径功能

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

@@ -2,6 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:cvt="clr-namespace:GeekDesk.Converts"
CornerRadius="4"
Width="350"
Height="450"
@@ -10,29 +11,39 @@
<Border.Resources>
<Style x:Key="LeftTB" TargetType="TextBlock" BasedOn="{StaticResource TextBlockBaseStyle}">
<Setter Property="Width" Value="75"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="TextAlignment" Value="Left"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0,8,0,0"/>
<Setter Property="Margin" Value="5,8,0,0"/>
<Setter Property="FontSize" Value="14"/>
</Style>
<cvt:StringAppendConvert x:Key="StringAppendConvert"/>
</Border.Resources>
<hc:SimplePanel Margin="10" VerticalAlignment="Center">
<StackPanel>
<Button Width="22" Height="22" Command="hc:ControlCommands.Close" Style="{StaticResource ButtonIcon}" Foreground="{DynamicResource {x:Static SystemColors.ControlDarkDarkBrushKey}}" hc:IconElement.Geometry="{StaticResource ErrorGeometry}" Padding="0" HorizontalAlignment="Right" VerticalAlignment="Top"/>
<hc:UniformSpacingPanel Spacing="10">
<hc:UniformSpacingPanel Spacing="10" Margin="0,15,0,0">
<TextBlock Text="名称:" Style="{StaticResource LeftTB}"/>
<TextBox x:Name="IconName" Text="{Binding Name, Mode=OneWay}" Width="230" FontSize="14"/>
</hc:UniformSpacingPanel>
<hc:Divider LineStrokeDashArray="3,3" LineStroke="Black"/>
<hc:UniformSpacingPanel Spacing="10" Grid.ColumnSpan="4">
<hc:UniformSpacingPanel Spacing="10" Margin="0,15,0,0">
<TextBlock Text="相对路径:" Style="{StaticResource LeftTB}"/>
<TextBlock Text="{Binding RelativePath, Mode=OneWay}"
VerticalAlignment="Center"
Margin="0,8,0,0"
Width="230"
FontSize="14"
TextTrimming="WordEllipsis"
hc:Poptip.Placement="Bottom"
hc:Poptip.Content="{Binding RelativePath, Mode=OneWay, Converter={StaticResource StringAppendConvert}, ConverterParameter='\{\}\\\n(同盘符下才会建立相对路径)'}"/>
</hc:UniformSpacingPanel>
<hc:UniformSpacingPanel Spacing="10" Grid.ColumnSpan="4" Margin="0,15,0,0">
<TextBlock Text="图标:" Style="{StaticResource LeftTB}"/>
<Image x:Name="IconImg" Source="{Binding BitmapImage, Mode=OneWay}" RenderOptions.BitmapScalingMode="HighQuality" Width="60" Height="60"/>
<Button Content="修改" Click="EditImage"/>
<Button Content="重置" Click="ReStoreImage"/>
</hc:UniformSpacingPanel>
<hc:Divider LineStrokeDashArray="3,3" LineStroke="Black" Grid.ColumnSpan="4"/>
<hc:UniformSpacingPanel Spacing="10">
<hc:UniformSpacingPanel Spacing="10" Margin="4,15,0,0">
<CheckBox x:Name="IconIsAdmin" Content="始终以管理员方式启动" IsChecked="{Binding AdminStartUp, Mode=OneWay}">
<CheckBox.Background>
<LinearGradientBrush EndPoint="1,0" StartPoint="0,0">
@@ -41,12 +52,11 @@
</CheckBox.Background>
</CheckBox>
</hc:UniformSpacingPanel>
<hc:Divider LineStrokeDashArray="3,3" LineStroke="Black" Grid.ColumnSpan="4"/>
<hc:UniformSpacingPanel Spacing="10">
<hc:UniformSpacingPanel Spacing="10" Margin="0,15,0,0">
<TextBlock Text="启动参数:" Style="{StaticResource LeftTB}"/>
<TextBox x:Name="StartArg" Text="{Binding StartArg, Mode=OneWay}" Width="230" Height="100" TextWrapping="Wrap" FontSize="14"/>
</hc:UniformSpacingPanel>
<hc:UniformSpacingPanel Spacing="10" Grid.ColumnSpan="4">
<hc:UniformSpacingPanel Margin="0,25,0,0" Spacing="10" Grid.ColumnSpan="4">
<Button Content="保存" Style="{StaticResource Btn1}" Click="SaveProperty" Margin="265,10,0,0"/>
</hc:UniformSpacingPanel>
</StackPanel>

View File

@@ -171,33 +171,51 @@ namespace GeekDesk.Control.UserControls.PannelCard
StartIconApp(icon, IconStartType.SHOW_IN_EXPLORE);
}
private void StartIconApp(IconInfo icon, IconStartType type)
private void StartIconApp(IconInfo icon, IconStartType type, bool useRelativePath = false)
{
try
{
using (Process p = new Process())
{
string startArg = icon.StartArg;
if (startArg != null && Constants.SYSTEM_ICONS.ContainsKey(startArg))
{
StartSystemApp(startArg, type);
}
else
{
p.StartInfo.FileName = icon.Path;
string path;
if (useRelativePath)
{
string fullPath = Path.Combine(Constants.APP_DIR, icon.RelativePath);
path = Path.GetFullPath(fullPath);
} else
{
path = icon.Path;
}
p.StartInfo.FileName = path;
if (!StringUtil.IsEmpty(startArg))
{
p.StartInfo.Arguments = startArg;
}
if (icon.IconType == IconType.OTHER)
{
if (!File.Exists(icon.Path) && !Directory.Exists(icon.Path))
if (!File.Exists(path) && !Directory.Exists(path))
{
HandyControl.Controls.Growl.WarningGlobal("程序启动失败(文件路径不存在或已删除)!");
return;
//如果没有使用相对路径 那么使用相对路径启动一次
if (!useRelativePath)
{
StartIconApp(icon, type, true);
return;
} else
{
HandyControl.Controls.Growl.WarningGlobal("程序启动失败(文件路径不存在或已删除)!");
return;
}
}
p.StartInfo.WorkingDirectory = icon.Path.Substring(0, icon.Path.LastIndexOf("\\"));
p.StartInfo.WorkingDirectory = path.Substring(0, path.LastIndexOf("\\"));
switch (type)
{
case IconStartType.ADMIN_STARTUP:
@@ -265,6 +283,11 @@ namespace GeekDesk.Control.UserControls.PannelCard
}
}
p.Start();
if (useRelativePath)
{
//如果使用相对路径启动成功 那么重新设置程序绝对路径
icon.Path = path;
}
}
}
icon.Count++;
@@ -277,8 +300,15 @@ namespace GeekDesk.Control.UserControls.PannelCard
}
catch (Exception e)
{
HandyControl.Controls.Growl.WarningGlobal("程序启动失败(不支持的启动方式)!");
LogUtil.WriteErrorLog(e, "程序启动失败:path=" + icon.Path + ",type=" + type);
if (!useRelativePath)
{
StartIconApp(icon, type, true);
}
else
{
HandyControl.Controls.Growl.WarningGlobal("程序启动失败(可能为不支持的启动方式)!");
LogUtil.WriteErrorLog(e, "程序启动失败:path=" + icon.Path + ",type=" + type);
}
}
}

View File

@@ -6,7 +6,6 @@
xmlns:local="clr-namespace:GeekDesk.Control.UserControls.PannelCard"
xmlns:hc="https://handyorg.github.io/handycontrol" xmlns:viewmodel="clr-namespace:GeekDesk.ViewModel" d:DataContext="{d:DesignInstance Type=viewmodel:ToDoInfo}"
mc:Ignorable="d"
xmlns:cst="clr-namespace:GeekDesk.Converts"
Background="Transparent"
>

View File

@@ -8,7 +8,10 @@ namespace GeekDesk.Converts
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return null;
if (value == null)
{
value = "";
}
if (parameter == null)
{
return value.ToString();
@@ -16,7 +19,14 @@ namespace GeekDesk.Converts
else
{
string val = value.ToString();
if (string.IsNullOrEmpty(val))
{
return parameter.ToString()
.Replace("\\n", "")
.Replace("{}", "");
}
string param = parameter.ToString();
param = param.Replace("\\n", "\n");
return param.Replace("{}", val);
}
}

View File

@@ -4,6 +4,6 @@ namespace GeekDesk.Interface
{
public interface IWindowCommon
{
void OnKeyDown(object sender, KeyEventArgs e);
void OnKeyDown(object sender, KeyEventArgs e);
}
}

View File

@@ -18,8 +18,6 @@ namespace GeekDesk.MyThread
{
new Thread(() =>
{
Thread.Sleep(1000);
ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
string myExePath = Constants.APP_DIR + "GeekDesk.exe";
@@ -28,7 +26,12 @@ namespace GeekDesk.MyThread
ObservableCollection<IconInfo> iconList = mi.IconList;
foreach (IconInfo icon in iconList)
{
icon.RelativePath_NoWrite = FileUtil.MakeRelativePath(myExePath, icon.Path);
string relativePath = FileUtil.MakeRelativePath(myExePath, icon.Path);
if (File.Exists(icon.Path)
&& !string.IsNullOrEmpty(relativePath)
&& !relativePath.Equals(icon.Path)) {
icon.RelativePath_NoWrite = relativePath;
}
}
}
CommonCode.SaveAppData(MainWindow.appData, Constants.DATA_FILE_PATH);

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;
}