@@ -18,6 +18,7 @@ public partial class FormMain : Form
1818 {
1919 private readonly string userDataPath = Path . Combine ( Application . StartupPath , "UserData" ) ;
2020 private string thisVersion ;
21+ private string installPath = FindInstallPathFromRegistry ( ) ;
2122 public FormMain ( )
2223 {
2324 InitializeComponent ( ) ;
@@ -35,8 +36,16 @@ private void FormMain_Load(object sender, EventArgs e)
3536 this . Text += currentVersion ;
3637 GAHelper . Instance . RequestPageView ( $ "/acct/main/{ thisVersion } ", $ "进入{ thisVersion } 版本原神账户切换工具主界面") ;
3738
39+
40+ chkAutoStartYS . Checked = Properties . Settings . Default . AutoRestartYSEnabled ;
41+ chkSkipTips . Checked = Properties . Settings . Default . SkipTipsEnabled ;
42+
3843 lvwAcct . Columns [ 0 ] . Width = lvwAcct . Width ;
44+ ImageList imageList = new ImageList ( ) ;
45+ imageList . ImageSize = new Size ( 10 , 20 ) ;
46+ lvwAcct . SmallImageList = imageList ;
3947 RefreshList ( ) ;
48+
4049 }
4150
4251 private void btnSaveCurr_Click ( object sender , EventArgs e )
@@ -63,7 +72,7 @@ private void RefreshList()
6372 } ) ;
6473 }
6574
66- if ( lvwAcct . Items . Count > 0 )
75+ if ( lvwAcct . Items . Count > 0 )
6776 {
6877 btnDelete . Enabled = true ;
6978 btnSwitch . Enabled = true ;
@@ -83,27 +92,66 @@ private void btnSwitch_Click(object sender, EventArgs e)
8392 return ;
8493 }
8594 string name = lvwAcct . SelectedItems [ 0 ] ? . Text ;
95+ Switch ( name , chkAutoStartYS . Checked ) ;
96+ }
97+
98+ private void Switch ( string name , bool autoRestart )
99+ {
86100 if ( string . IsNullOrEmpty ( name ) )
87101 {
88102 MessageBox . Show ( "请选择要切换的账号" , "提示" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
89103 return ;
90104 }
91- if ( YuanShenIsRunning ( ) )
105+ if ( ! autoRestart )
92106 {
93- MessageBox . Show ( "原神正在运行,请先关闭原神进程后再切换账号!" , "提示" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
94- return ;
107+ if ( YuanShenIsRunning ( ) )
108+ {
109+ MessageBox . Show ( "原神正在运行,请先关闭原神进程后再切换账号!" , "提示" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
110+ return ;
111+ }
95112 }
96- if ( MessageBox . Show ( $ "是否要切换为[{ name } ]", "提示" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) == DialogResult . Yes )
113+
114+ if ( chkSkipTips . Checked || MessageBox . Show ( $ "是否要切换为[{ name } ]", "提示" , MessageBoxButtons . YesNo , MessageBoxIcon . Question ) == DialogResult . Yes )
97115 {
116+ if ( autoRestart )
117+ {
118+ var pros = Process . GetProcessesByName ( "YuanShen" ) ;
119+ if ( pros . Any ( ) )
120+ {
121+ pros [ 0 ] . Kill ( ) ;
122+ }
123+ }
98124 YSAccount acct = YSAccount . ReadFromDisk ( name ) ;
99125 acct . WriteToRegedit ( ) ;
100- }
101126
127+ if ( autoRestart )
128+ {
129+ if ( string . IsNullOrEmpty ( installPath ) )
130+ {
131+ MessageBox . Show ( "未找到原神安装信息,无法自动启动原神" , "提示" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
132+ chkAutoStartYS . Checked = false ;
133+ }
134+ else
135+ {
136+ ProcessStartInfo startInfo = new ProcessStartInfo ( ) ;
137+ startInfo . UseShellExecute = true ;
138+ startInfo . WorkingDirectory = Environment . CurrentDirectory ;
139+ startInfo . FileName = Path . Combine ( installPath , "Genshin Impact Game" , "YuanShen.exe" ) ;
140+ startInfo . Verb = "runas" ;
141+ Process . Start ( startInfo ) ;
142+ }
143+ }
144+
145+ if ( ! chkSkipTips . Checked )
146+ {
147+ MessageBox . Show ( $ "账户[{ name } ]切换成功", "提示" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
148+ }
149+ }
102150 }
103151
104152 private void btnDelete_Click ( object sender , EventArgs e )
105153 {
106- if ( lvwAcct . SelectedItems . Count == 0 )
154+ if ( lvwAcct . SelectedItems . Count == 0 )
107155 {
108156 MessageBox . Show ( "请选择要切换的账号" , "提示" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
109157 return ;
@@ -136,5 +184,53 @@ private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
136184 {
137185 Process . Start ( "https://github.com/babalae/genshin-account" ) ;
138186 }
187+
188+ private void lvwAcct_MouseDoubleClick ( object sender , MouseEventArgs e )
189+ {
190+ ListViewHitTestInfo info = lvwAcct . HitTest ( e . X , e . Y ) ;
191+ if ( info . Item != null )
192+ {
193+ Switch ( info . Item . Text , chkAutoStartYS . Checked ) ;
194+ }
195+ }
196+
197+ /// <summary>
198+ /// 从注册表中寻找安装路径
199+ /// </summary>
200+ /// <param name="uninstallKeyName">
201+ /// 安装信息的注册表键名 原神
202+ /// </param>
203+ /// <returns>安装路径</returns>
204+ public static string FindInstallPathFromRegistry ( )
205+ {
206+ try
207+ {
208+ using ( var hklm = RegistryKey . OpenBaseKey ( RegistryHive . LocalMachine , RegistryView . Registry64 ) )
209+ using ( var key = hklm . OpenSubKey ( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\原神" ) )
210+ {
211+ if ( key == null )
212+ {
213+ return null ;
214+ }
215+ object installLocation = key . GetValue ( "InstallPath" ) ;
216+ if ( installLocation != null && ! string . IsNullOrEmpty ( installLocation . ToString ( ) ) )
217+ {
218+ return installLocation . ToString ( ) ;
219+ }
220+ }
221+ }
222+ catch ( Exception e )
223+ {
224+ Console . WriteLine ( e . Message ) ;
225+ }
226+ return null ;
227+ }
228+
229+ private void FormMain_FormClosed ( object sender , FormClosedEventArgs e )
230+ {
231+ Properties . Settings . Default . AutoRestartYSEnabled = chkAutoStartYS . Checked ;
232+ Properties . Settings . Default . SkipTipsEnabled = chkSkipTips . Checked ;
233+ Properties . Settings . Default . Save ( ) ;
234+ }
139235 }
140236}
0 commit comments