0

我想在没有计时器访问的情况下关闭 winform 时自动注销。我怎么做?

4

1 回答 1

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
}

谢谢,
我希望你觉得这有帮助:)

于 2012-11-01T08:13:11.587 回答