Skip to content

Commit 902416f

Browse files
committed
Fix Reg Issues (Windows XP Support)
1 parent 069f2bf commit 902416f

4 files changed

Lines changed: 77 additions & 41 deletions

File tree

ComTransfer/ComPort.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class ComPort : CustomINotifyPropertyChanged
9292
/// <summary>
9393
/// 串口文件保存地址设置
9494
/// </summary>
95-
public List<string> PortOption => directoryDict.Select(item => string.Format("{0}>[{1}]", item.Key, item.Value)).ToList();
95+
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();
9696
/// <summary>
9797
/// 文件保存地址字典
9898
/// </summary>

ComTransfer/Utility.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,15 @@ public class IsMoreThanConverter : IValueConverter
4646
{
4747
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
4848
{
49-
bool flag = (double)value > double.Parse(parameter.ToString());
50-
return flag;
49+
try
50+
{
51+
bool flag = (double)value > double.Parse(parameter.ToString());
52+
return flag;
53+
}
54+
catch
55+
{
56+
return false;
57+
}
5158
}
5259

5360
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

ComTransfer/View/ConfigWindow.xaml.cs

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,7 @@ namespace ComTransfer
2323
/// </summary>
2424
public partial class ConfigWindow : Window
2525
{
26-
private static readonly bool IsWindowsXP = Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1;
27-
/// <summary>
28-
/// 启动运行注册表位置
29-
/// </summary>
30-
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);
26+
private static readonly bool IsWindowsXP = true || Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1;
3127
public int PortID { get; set; } = 1;
3228
public int BaudRate { get; set; } = 9600;
3329
public int DataBits { get; set; } = 8;
@@ -43,41 +39,58 @@ public bool IsAutoStart
4339
{
4440
get
4541
{
46-
if (IsWindowsXP)
42+
try
4743
{
48-
return regPath.GetValue("shell") != null && regPath.GetValue("shell").ToString().Contains(Process.GetCurrentProcess().MainModule.FileName);
44+
RegistryKey regPath = IsWindowsXP ? Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", true) : Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
45+
if (IsWindowsXP)
46+
{
47+
return regPath.GetValue("Shell") != null && regPath.GetValue("Shell").ToString().Contains(Process.GetCurrentProcess().MainModule.FileName);
48+
}
49+
return regPath.GetValue(Process.GetCurrentProcess().ProcessName) != null;
50+
}
51+
catch (Exception e)
52+
{
53+
MessageBox.Show(e.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
54+
return false;
4955
}
50-
return regPath.GetValue(Process.GetCurrentProcess().ProcessName) != null;
5156
}
5257
set
5358
{
54-
if (IsWindowsXP)
59+
try
5560
{
56-
string text = regPath.GetValue("shell")?.ToString();
57-
if (value)
61+
RegistryKey regPath = IsWindowsXP ? Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon", true) : Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
62+
if (IsWindowsXP)
5863
{
59-
regPath.SetValue("shell", text == null ? Process.GetCurrentProcess().MainModule.FileName : string.Join(",", text, Process.GetCurrentProcess().MainModule.FileName));
64+
string text = regPath.GetValue("Shell")?.ToString();
65+
if (value)
66+
{
67+
regPath.SetValue("Shell", text == null ? Process.GetCurrentProcess().MainModule.FileName : string.Join(",", text, Process.GetCurrentProcess().MainModule.FileName));
68+
}
69+
else
70+
{
71+
if (text == null)
72+
{
73+
return;
74+
}
75+
var frags = text.Split(',').Where(item => !item.Contains(Process.GetCurrentProcess().MainModule.FileName)).ToList();
76+
regPath.SetValue("Shell", string.Join(",", frags));
77+
}
6078
}
6179
else
6280
{
63-
if (text == null)
81+
if (value)
6482
{
65-
return;
83+
regPath.SetValue(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName);
84+
}
85+
else
86+
{
87+
regPath.DeleteValue(Process.GetCurrentProcess().ProcessName, false);
6688
}
67-
var frags = text.Split(',').Where(item => !item.Contains(Process.GetCurrentProcess().MainModule.FileName)).ToList();
68-
regPath.SetValue("shell", string.Join(",", frags));
6989
}
7090
}
71-
else
91+
catch (Exception e)
7292
{
73-
if (value)
74-
{
75-
regPath.SetValue(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName);
76-
}
77-
else
78-
{
79-
regPath.DeleteValue(Process.GetCurrentProcess().ProcessName, false);
80-
}
93+
MessageBox.Show(e.Message, "提示", MessageBoxButton.OK, MessageBoxImage.Information);
8194
}
8295
}
8396
}

ComTransfer/View/MainWindow.xaml

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
</Control.Resources>
6262
<Control.Template>
6363
<ControlTemplate>
64-
<Grid x:Name="Grid" Margin="4,2">
64+
<Grid x:Name="Grid" Margin="4,2" d:DataContext="{d:DesignInstance Type=local:ComPort}">
6565
<Grid.ColumnDefinitions>
6666
<ColumnDefinition Width="*"/>
6767
<ColumnDefinition Width="0" x:Name="GridColumnSecond"/>
@@ -130,18 +130,34 @@
130130
<ColumnDefinition Width="Auto"/>
131131
</Grid.ColumnDefinitions>
132132
<Label Grid.Column="0" Content="程序设置"/>
133-
<ItemsControl Grid.Column="1" Margin="4,0" VerticalAlignment="Stretch" ItemsSource="{Binding PortOption}">
134-
<ItemsControl.ItemsPanel>
135-
<ItemsPanelTemplate>
136-
<StackPanel Orientation="Horizontal"/>
137-
</ItemsPanelTemplate>
138-
</ItemsControl.ItemsPanel>
139-
<ItemsControl.ItemTemplate>
140-
<DataTemplate>
141-
<Label Content="{Binding}" Style="{StaticResource Label_Status}" Background="#FFEE44" BorderBrush="#DDAA22" Foreground="#334466" Margin="0,0,4,0" ToolTip="存储目标地址" MouseDown="Label_Folder_MouseDown"/>
142-
</DataTemplate>
143-
</ItemsControl.ItemTemplate>
144-
</ItemsControl>
133+
<Border Grid.Column="1" Margin="4,0">
134+
<ItemsControl VerticalAlignment="Stretch" ItemsSource="{Binding PortOption}">
135+
<ItemsControl.ItemsPanel>
136+
<ItemsPanelTemplate>
137+
<StackPanel Orientation="Horizontal"/>
138+
</ItemsPanelTemplate>
139+
</ItemsControl.ItemsPanel>
140+
<ItemsControl.ItemTemplate>
141+
<DataTemplate>
142+
<Label Content="{Binding Value}" Margin="0,0,4,0" ToolTip="存储目标地址" MouseDown="Label_Folder_MouseDown">
143+
<Label.Style>
144+
<Style TargetType="Label">
145+
<Setter Property="Height" Value="24"/>
146+
<Setter Property="Padding" Value="4,2"/>
147+
<Setter Property="Margin" Value="0,0,4,0"/>
148+
<Setter Property="BorderThickness" Value="1"/>
149+
<Setter Property="BorderBrush" Value="#DDAA22"/>
150+
<Setter Property="Background" Value="#FFEE44"/>
151+
<Setter Property="Foreground" Value="#334466"/>
152+
<Setter Property="VerticalAlignment" Value="Center"/>
153+
<Setter Property="VerticalContentAlignment" Value="Center"/>
154+
</Style>
155+
</Label.Style>
156+
</Label>
157+
</DataTemplate>
158+
</ItemsControl.ItemTemplate>
159+
</ItemsControl>
160+
</Border>
145161
<Border Grid.Column="2">
146162
<Button Grid.Column="2" Click="Button_Option_Click" ToolTip="程序设置">
147163
<Path Data="M1 13L6 8A5 5 0 0 1 9 1L12 1L10 4L12 6L15 3A5 5 0 0 1 9 10L4 15"/>

0 commit comments

Comments
 (0)