@@ -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 }
0 commit comments