Skip to content

Commit

Permalink
Update Windows XP Support for Auto Start
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinerising committed Mar 25, 2024
1 parent 4da1281 commit fe29eca
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions ComTransfer/View/ConfigWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ 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 = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
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);
public int PortID { get; set; } = 1;
public int BaudRate { get; set; } = 9600;
public int DataBits { get; set; } = 8;
Expand All @@ -42,17 +43,41 @@ public bool IsAutoStart
{
get
{
if (IsWindowsXP)
{
return regPath.GetValue("shell") != null && regPath.GetValue("shell").ToString().Contains(Process.GetCurrentProcess().MainModule.FileName);
}
return regPath.GetValue(Process.GetCurrentProcess().ProcessName) != null;
}
set
{
if (value)
if (IsWindowsXP)
{
regPath.SetValue(Process.GetCurrentProcess().ProcessName, 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
{
regPath.DeleteValue(Process.GetCurrentProcess().ProcessName, false);
if (value)
{
regPath.SetValue(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName);
}
else
{
regPath.DeleteValue(Process.GetCurrentProcess().ProcessName, false);
}
}
}
}
Expand Down

0 comments on commit fe29eca

Please sign in to comment.