Skip to content

Commit

Permalink
Fix Reg Issues (Windows XP Support)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinerising committed Mar 28, 2024
1 parent 069f2bf commit 902416f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 41 deletions.
2 changes: 1 addition & 1 deletion ComTransfer/ComPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class ComPort : CustomINotifyPropertyChanged
/// <summary>
/// 串口文件保存地址设置
/// </summary>
public List<string> PortOption => directoryDict.Select(item => string.Format("{0}>[{1}]", item.Key, item.Value)).ToList();
public List<KeyValuePair<string, string>> PortOption => directoryDict.Count == 0 ? new List<KeyValuePair<string, string>>() : directoryDict.Keys.Select(item => new KeyValuePair<string, string>(item, string.Format("{0}>[{1}]", item, directoryDict[item]))).ToList();
/// <summary>
/// 文件保存地址字典
/// </summary>
Expand Down
11 changes: 9 additions & 2 deletions ComTransfer/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ public class IsMoreThanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool flag = (double)value > double.Parse(parameter.ToString());
return flag;
try
{
bool flag = (double)value > double.Parse(parameter.ToString());
return flag;
}
catch
{
return false;
}
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
Expand Down
63 changes: 38 additions & 25 deletions ComTransfer/View/ConfigWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ namespace ComTransfer
/// </summary>
public partial class ConfigWindow : Window
{
private static readonly bool IsWindowsXP = Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1;
/// <summary>
/// 启动运行注册表位置
/// </summary>
private static readonly RegistryKey regPath = IsWindowsXP ? Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", true) : Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
private static readonly bool IsWindowsXP = true || Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1;
public int PortID { get; set; } = 1;
public int BaudRate { get; set; } = 9600;
public int DataBits { get; set; } = 8;
Expand All @@ -43,41 +39,58 @@ public bool IsAutoStart
{
get
{
if (IsWindowsXP)
try
{
return regPath.GetValue("shell") != null && regPath.GetValue("shell").ToString().Contains(Process.GetCurrentProcess().MainModule.FileName);
RegistryKey regPath = IsWindowsXP ? Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", true) : Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (IsWindowsXP)
{
return regPath.GetValue("Shell") != null && regPath.GetValue("Shell").ToString().Contains(Process.GetCurrentProcess().MainModule.FileName);
}
return regPath.GetValue(Process.GetCurrentProcess().ProcessName) != null;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return false;
}
return regPath.GetValue(Process.GetCurrentProcess().ProcessName) != null;
}
set
{
if (IsWindowsXP)
try
{
string text = regPath.GetValue("shell")?.ToString();
if (value)
RegistryKey regPath = IsWindowsXP ? Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", true) : Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (IsWindowsXP)
{
regPath.SetValue("shell", text == null ? Process.GetCurrentProcess().MainModule.FileName : string.Join(",", text, Process.GetCurrentProcess().MainModule.FileName));
string text = regPath.GetValue("Shell")?.ToString();
if (value)
{
regPath.SetValue("Shell", text == null ? Process.GetCurrentProcess().MainModule.FileName : string.Join(",", text, Process.GetCurrentProcess().MainModule.FileName));
}
else
{
if (text == null)
{
return;
}
var frags = text.Split(',').Where(item => !item.Contains(Process.GetCurrentProcess().MainModule.FileName)).ToList();
regPath.SetValue("Shell", string.Join(",", frags));
}
}
else
{
if (text == null)
if (value)
{
return;
regPath.SetValue(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName);
}
else
{
regPath.DeleteValue(Process.GetCurrentProcess().ProcessName, false);
}
var frags = text.Split(',').Where(item => !item.Contains(Process.GetCurrentProcess().MainModule.FileName)).ToList();
regPath.SetValue("shell", string.Join(",", frags));
}
}
else
catch (Exception e)
{
if (value)
{
regPath.SetValue(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName);
}
else
{
regPath.DeleteValue(Process.GetCurrentProcess().ProcessName, false);
}
MessageBox.Show(e.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
Expand Down
42 changes: 29 additions & 13 deletions ComTransfer/View/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
</Control.Resources>
<Control.Template>
<ControlTemplate>
<Grid x:Name="Grid" Margin="4,2">
<Grid x:Name="Grid" Margin="4,2" d:DataContext="{d:DesignInstance Type=local:ComPort}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="0" x:Name="GridColumnSecond"/>
Expand Down Expand Up @@ -130,18 +130,34 @@
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="程序设置"/>
<ItemsControl Grid.Column="1" Margin="4,0" VerticalAlignment="Stretch" ItemsSource="{Binding PortOption}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding}" Style="{StaticResource Label_Status}" Background="#FFEE44" BorderBrush="#DDAA22" Foreground="#334466" Margin="0,0,4,0" ToolTip="存储目标地址" MouseDown="Label_Folder_MouseDown"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Border Grid.Column="1" Margin="4,0">
<ItemsControl VerticalAlignment="Stretch" ItemsSource="{Binding PortOption}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Value}" Margin="0,0,4,0" ToolTip="存储目标地址" MouseDown="Label_Folder_MouseDown">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Height" Value="24"/>
<Setter Property="Padding" Value="4,2"/>
<Setter Property="Margin" Value="0,0,4,0"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="#DDAA22"/>
<Setter Property="Background" Value="#FFEE44"/>
<Setter Property="Foreground" Value="#334466"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
</Label.Style>
</Label>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
<Border Grid.Column="2">
<Button Grid.Column="2" Click="Button_Option_Click" ToolTip="程序设置">
<Path Data="M1 13L6 8A5 5 0 0 1 9 1L12 1L10 4L12 6L15 3A5 5 0 0 1 9 10L4 15"/>
Expand Down

0 comments on commit 902416f

Please sign in to comment.