【C#】C#+Winform 实现切换用户功能

方法一和方法二均为退出主进程后,重新开启新进程。方法三为不退出主进程,将窗体隐藏,切换用户时,重新新建一个窗体。被隐藏的窗体与新建的窗体同在一个线程下。

方法一:

 

#region 登录部分
private void RtnLoginOK_Click(object sender, EventArgs e)	//登录按钮单击事件
{
	this.Hide();
	FrmRoutineMain formRtnLoginOK = new FrmRoutineMain();	//新建主窗体
	formRtnLoginOK.ShowDialog();
}
#endregion

#region 切换用户
private void RtnMainSwitchUser_Click(object sender, EventArgs e)	//切换用户按钮单击事件
{
	if (DialogResult.Yes == MessageBox.Show("您确定要退出登陆吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
	{
		System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, "+自己程序设置的启动参数,没有则省略");
		Process.GetCurrentProcess().Kill();
	}
}
#endregion	//登录按钮单击事件
{
	this.Hide();
	FrmRoutineMain formRtnLoginOK = new FrmRoutineMain();	//新建主窗体
	formRtnLoginOK.ShowDialog();
}
#endregion

#region 切换用户
private void RtnMainSwitchUser_Click(object sender, EventArgs e)	//切换用户按钮单击事件
{
	if (DialogResult.Yes == MessageBox.Show("您确定要退出登陆吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
	{
		System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, "+自己程序设置的启动参数,没有则省略");
		Process.GetCurrentProcess().Kill();
	}
}
#endregion

 

 

方法二:

 

#region 登录部分
private void RtnLoginOK_Click(object sender, EventArgs e)	//登录按钮单击事件
{
	this.Hide();
	FrmRoutineMain formRtnLoginOK = new FrmRoutineMain();	//新建主窗体
	formRtnLoginOK.ShowDialog();
}
#endregion

#region 切换用户
private void RtnMainSwitchUser_Click(object sender, EventArgs e)
{
	if (DialogResult.Yes == MessageBox.Show("您确定要退出登陆吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
	{
		StartExe(Application.ExecutablePath);
		Thread.Sleep(1000);
		Application.ExitThread();
	}
}

private static void StartExe(string appName)
{
	string path = appName;
	Process ps = new Process();
	ps.StartInfo.FileName = path;
	ps.StartInfo.Arguments = "-routine";
	ps.StartInfo.CreateNoWindow = true;
	ps.StartInfo.WorkingDirectory = Path.GetDirectoryName(path);
	ps.Start();
}
#endregion	//登录按钮单击事件
{
	this.Hide();
	FrmRoutineMain formRtnLoginOK = new FrmRoutineMain();	//新建主窗体
	formRtnLoginOK.ShowDialog();
}
#endregion

#region 切换用户
private void RtnMainSwitchUser_Click(object sender, EventArgs e)
{
	if (DialogResult.Yes == MessageBox.Show("您确定要退出登陆吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
	{
		StartExe(Application.ExecutablePath);
		Thread.Sleep(1000);
		Application.ExitThread();
	}
}

private static void StartExe(string appName)
{
	string path = appName;
	Process ps = new Process();
	ps.StartInfo.FileName = path;
	ps.StartInfo.Arguments = "-routine";
	ps.StartInfo.CreateNoWindow = true;
	ps.StartInfo.WorkingDirectory = Path.GetDirectoryName(path);
	ps.Start();
}
#endregion


方法三:

 

 

 

 

#region 登录部分
private void RtnLoginOK_Click(object sender, EventArgs e) //登录按钮单击事件 { this.Hide(); FrmRoutineMain formRtnLoginOK = new FrmRoutineMain(); //新建主窗体 formRtnLoginOK.ShowDialog(); }

#endregion#region 切换用户private void RtnMainSwitchUser_Click(object sender, EventArgs e){if (DialogResult.Yes == MessageBox.Show("您确定要退出登陆吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)){this.Hide();Form NewForm = new FrmRoutineLogin(); //新建登录窗体NewForm.ShowDialog(); }}#endregion

 

 

 

 

 

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值