C#winform 判断是开机启动还是双击手动启动。如果是开机自启动,则窗体最小化,如果是双击启动则正常显示窗体。总算自己搞定了。具体的办法,加入命令行参数到注册表启动项里面。
[C#] 纯文本查看 复制代码 private void SetAutoStar()
{
try
{
string filepath = Assembly.GetExecutingAssembly().Location;
string runName = Path.GetFileNameWithoutExtension(filepath);
RegistryKey hkml = Registry.LocalMachine;
RegistryKey runKey = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
runKey.SetValue("xcgk", "\"" + filepath + "\" -AutoRun");
runKey.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
判断启动方式:
[C#] 纯文本查看 复制代码 string AutoRun = "";
try
{
AutoRun=Environment.GetCommandLineArgs()[1];//以此判断是否自动开机或者手动启动。
}
catch
{
}
if (AutoRun !="")//如果是从注册表启动,则开机启动之后最小化
{
this.WindowState = FormWindowState.Minimized;
}else
{
return;
}
|