关闭winform时自动注销

时间:2012-11-01 06:13:03

标签: c#

我希望在没有定时器访问权限的情况下关闭winform时进行自动注销。我该怎么做?

1 个答案:

答案 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
}

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