这段代码有什么问题 [英] Whats wrong with this code

查看:74
本文介绍了这段代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把这段代码借给了我的应用程序。此应用程序在公共库上运行

计算机以监视每个用户在计算机上花费的时间,因此我们不希望用户关闭该应用程序。我使用此代码发布了应用程序,并且当我以管理员身份登录时禁用了Alt + F4,这是我想要的,但在其他任何用户登录时都不会这样做。看不到一段代码如何在一个用户而不是另一个用户下运行。 Ist it ppossible



  protected  覆盖  void  OnFormClosing(FormClosingEventArgs e)
{
switch (e.CloseReason)
{
case CloseReason.UserClosing:
e.Cancel = ;
return ;
}
base .OnFormClosing(e);
}

解决方案

当您理清e.CloseReason发生的事情时,您可以实施热修复对于Alt-F4,将Form的KeyPreview属性设置为true,并将KeyDown事件连接起来:

  private   void  Form1_KeyDown( object  sender,KeyEventArgs e)
{
e.Handled =(e.Alt = = true && e.KeyCode == Keys.F4);
}


Sooooo ...当用户启动任务管理器并通过强制关闭你的应用程序时你打算做什么?你的应用程序中的代码不会阻止这种情况发生。



你也不能在其中放置任何代码以防止用户运行命令行工具TASKLIST和TASKKILL

I borrowed this piece of code into my application. This application runs on public library
computers to monitor time each user spends on a computer, so we don't want users closing the application. I published the application with this code and it disables Alt+F4 when I log in as administrator which is what I want but does not do so when any other user logs on . Don't see how a piece of code can run under one user and not another. Ist it ppossible

protected override void OnFormClosing(FormClosingEventArgs e)
        {
            switch (e.CloseReason)
            {
                case CloseReason.UserClosing:
                    e.Cancel = true;
                    return;
            }
            base.OnFormClosing(e);
        }

解决方案

While you sort out what's going on with e.CloseReason, you could implement a "hot-fix" for Alt-F4 by setting the Form's KeyPreview Property to 'true, and wiring-up the KeyDown Event:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    e.Handled = (e.Alt == true && e.KeyCode == Keys.F4);
}


Sooooo...what are you going to do when the user launches Task Manager and closes your app by force?? The code in your app won't stop that from happening.

You also cannot put any code in there to prevent a user from running the command line tools TASKLIST and TASKKILL.


这篇关于这段代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆