123
返回列表 发新帖
楼主: aquablue
收起左侧

[系统] Win7怎样防止管理员登录在线的情况下被标准用户关机?

[复制链接]
aquablue
 楼主| 发表于 2014-8-26 13:41:05 | 显示全部楼层
thelord 发表于 2014-8-26 13:16
不该填ceshi,填“任务管理器--用户”里的管理员名称,即要监视登陆的用户

xml文件修改为管理员账户,测试了,发现提示信息没有变化,可以见截图,不知道是我这里哪里设置得不对?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?快速注册

x
linyinlu
发表于 2014-8-26 14:28:42 | 显示全部楼层
aquablue 发表于 2014-8-26 13:41
xml文件修改为管理员账户,测试了,发现提示信息没有变化,可以见截图,不知道是我这里哪里设置得不对?

看了一下,由于thelord的程序下载时被Chrome阻止,无法进行测试,所以也不知道代码上是否有问题。我只是想问:
1. Administrator看到状态是“已断开”,你是注销了还是“切换用户”?
2. config.xml修改以后是否保存了?因为看到背景窗口中,后缀名为“lock_”的文件的大小还是0KB。这个文件应当是在你编辑xml文件时自动生成的隐藏文件,在没有保存前不会消失。
aquablue
 楼主| 发表于 2014-8-26 14:36:11 | 显示全部楼层
linyinlu 发表于 2014-8-26 14:28
看了一下,由于thelord的程序下载时被Chrome阻止,无法进行测试,所以也不知道代码上是否有问题。我只是 ...

1、我当时是切换用户了,没有注销。2、xml修改后应该是保存了的,我是右键“编辑”后再修改的,修改后保存,关闭。
thelord
发表于 2014-8-26 15:46:18 | 显示全部楼层
本帖最后由 thelord 于 2014-8-26 15:49 编辑

@linyinlu,@aquablue

生成 __shutdown.lock__ 文件说明已经检测到 adminstrator 用户登陆,配置没问题。现在的问题是点确定后会不会有关机被阻挡的提示

代码如下,欢迎讨论:

[mw_shl_code=javascript,true]
using System;
using System.Windows.Forms;

using System.Diagnostics;   //进程类
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;

using Microsoft.Win32;

namespace WFBeforeShutdown
{
    public partial class MainForm : Form
    {
        private string user2check;
        private string marker = "__shutdown.lock__";
        private Timer timer = new Timer();

        public MainForm()
        {
            InitializeComponent();
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;

            // 自定义关机和logoff的事件
            this.Text = "关机提示";
            SystemEvents.SessionEnding += new SessionEndingEventHandler(this.SystemEvents_SessionEnding);
            // 检查定时器
            timer.Interval = 5000;
            timer.Tick += new EventHandler(CheckAction);
            timer.Enabled = true;
        }

        internal void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
        {
            timer.Stop();
            if (e.Reason == SessionEndReasons.SystemShutdown)
            {
                e.Cancel = File.Exists(marker);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            //this.Hide(); // 会接收不到事件?
            LoadConfigFile();
            base.OnLoad(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            SystemEvents.SessionEnding -= new SessionEndingEventHandler(this.SystemEvents_SessionEnding);
            base.OnClosed(e);
        }

        void CheckAction(object sender, EventArgs e)
        {
            try
            {
                if (IsInactiveUser(user2check))
                {
                    if (!File.Exists(marker))
                    {
                        File.Create(marker);
                    }
                }
                else
                {
                    if (File.Exists(marker))
                    {
                        File.Delete(marker);
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
            }
        }

        public bool IsInactiveUser(string userName)
        {
            if (userName.Length == 0)
            {
                return false;
            }

            string sessionQuery;
            string cmd = "qwinsta.exe";
            execute(cmd, "", out sessionQuery);
            string pattern = @"(?<!>console[ ]+)(" + userName + @")[ ]+[0-9]+";
            bool match = Regex.IsMatch(sessionQuery, pattern);

            return match;
        }

        public void LoadConfigFile()
        {
            try
            {
                XmlReader reader = XmlReader.Create("config.xml");
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "user")
                    {
                        user2check = reader.ReadElementString();
                        break;
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Error: Load config file fail! {0}", ex.Message);
                System.Environment.Exit(0);
            }
        }

        private void execute(string cmd, string args, out string strRst)
        {
            var proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = cmd,
                    Arguments = args,
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true
                }
            };

            proc.Start();
            proc.WaitForExit();
            strRst = proc.StandardOutput.ReadToEnd();
            //MessageBox.Show(strRst);
            proc.Close();
        }
    }
}[/mw_shl_code]

评分

参与人数 1经验 +6 收起 理由
woxihuan2011 + 6 感谢提供分享

查看全部评分

aquablue
 楼主| 发表于 2014-8-26 17:14:44 | 显示全部楼层
thelord 发表于 2014-8-26 15:46
@linyinlu,@aquablue

生成 __shutdown.lock__ 文件说明已经检测到 adminstrator 用户登陆,配置没问题 ...

经测试,有关机阻挡的提示。是对的。谢谢。
thelord
发表于 2014-8-26 17:49:27 | 显示全部楼层
aquablue 发表于 2014-8-26 17:14
经测试,有关机阻挡的提示。是对的。谢谢。

en,有效就好。
您需要登录后才可以回帖 登录 | 快速注册

本版积分规则

手机版|杀毒软件|软件论坛| 卡饭论坛

Copyright © KaFan  KaFan.cn All Rights Reserved.

Powered by Discuz! X3.4( 沪ICP备2020031077号-2 ) GMT+8, 2024-7-5 22:16 , Processed in 0.092785 second(s), 15 queries .

卡饭网所发布的一切软件、样本、工具、文章等仅限用于学习和研究,不得将上述内容用于商业或者其他非法用途,否则产生的一切后果自负,本站信息来自网络,版权争议问题与本站无关,您必须在下载后的24小时之内从您的电脑中彻底删除上述信息,如有问题请通过邮件与我们联系。

快速回复 客服 返回顶部 返回列表