我希望在没有定时器访问权限的情况下关闭winform时进行自动注销。我该怎么做?
答案 0 :(得分:0)
我真的不明白without timer access
是什么意思,但您可以在表单使用FormClosing
事件关闭时运行流程或命令。
如果您希望在表单即将关闭时注销用户,您可以尝试将可执行文件shutdown.exe
用作进程及其参数/l
示例强>
public Form1()
{
InitializeComponent();
this.FormClosing += new FormClosingEventHandler(Form1_FormClosing); //Link FormClosing event to Form1_FormClosing
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Process logoff = new Process(); //Initialize a new process
ProcessStartInfo ProcessInfo = new ProcessStartInfo(); //Initialize a new ProcessStartInfo
ProcessInfo.FileName = "shutdown.exe"; //Set the FileName of ProcessInfo
ProcessInfo.Arguments = "/l"; //Log off, see 'shutdown.exe /?' for more information
//ProcessInfo.WindowStyle = ProcessWindowStyle.Hidden; //Hide the process window (not required)
logoff.StartInfo = ProcessInfo; //Associate ProcessInfo with logoff.StartInfo
logoff.Start(); //Start the process
}
这将启动可执行文件shutdown.exe
作为一个新进程,其参数为/l
,这意味着注销当前用户
请注意:表单即将关闭时会触发 FormClosing
。您可以通过将e.Cancel
设置为true
示例强>
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true; //Don't close the form
}
谢谢, 我希望你觉得这很有帮助:)