系统空闲时间判断&命名验证

一、系统空闲时间判断

需要一个自动登录注销的功能,当鼠标移动和或者键盘输入的时候认为当前用户在线,否则过了设置时间就自动退出。好在前辈们留下了这样的一个类:

MouseKeyBoardOperate:

using System;
using System.Runtime.InteropServices;

namespace SCADA.RTDB.Framework.Helpers
{
    /// <summary>
    /// Class MouseKeyBoardOperate
    /// </summary>
   public class MouseKeyBoardOperate
    {
        /// <summary>
        /// 创建结构体用于返回捕获时间
        /// </summary>LayoutKind.Sequential 用于强制将成员按其出现的顺序进行顺序布局。
        [StructLayout(LayoutKind.Sequential)]
        struct LASTINPUTINFO
        {
            /// <summary>
            /// 设置结构体块容量
            /// </summary>MarshalAs属性指示如何在托管代码和非托管代码之间封送数据。
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;

            /// <summary>
            /// 抓获的时间
            /// </summary>
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }

        static LASTINPUTINFO vLastInputInfo;
        public MouseKeyBoardOperate()
        {
            vLastInputInfo = new LASTINPUTINFO();
        }

        [DllImport("user32.dll")]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
        /// <summary>
        /// 获取键盘和鼠标没有操作的时间
        /// </summary>
        /// <returns>用户上次使用系统到现在的时间间隔,单位为秒</returns>
        public static long GetLastInputTime()
        {
            //LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
            vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
            if (!GetLastInputInfo(ref vLastInputInfo))
            {
                return 0;
            }
            else
            {
                long count = Environment.TickCount - (long)vLastInputInfo.dwTime;
                long icount = count / 1000;
                return icount;
            }
        }
    }
}

调用MouseKeyBoardOperate.GetLastInputTime() 就可以获得当前空闲的秒数,鼠标移动或者键盘输入这个值马上会变成0。

二、命名验证。

当模型对象的Name set的时候,我们需要验证其合法性,不能有特别的字符,不能数字开头。

 public class NameValidationHelper
    {
        public static bool IsValidIdentifierName(string name)
        {
            // Grammar:
            // <identifier> ::= <identifier_start> ( <identifier_start> | <identifier_extend> )*
            // <identifier_start> ::= [{Lu}{Ll}{Lt}{Lo}{Nl}(‘_‘)]
            // <identifier_extend> ::= [{Mn}{Mc}{Lm}{Nd}]
            UnicodeCategory uc;
            for (int i = 0; i < name.Length; i++)
            {
                uc = Char.GetUnicodeCategory(name[i]);
                bool idStart = (uc == UnicodeCategory.UppercaseLetter || // (Lu)
                                uc == UnicodeCategory.LowercaseLetter || // (Ll)
                                uc == UnicodeCategory.TitlecaseLetter || // (Lt)
                                uc == UnicodeCategory.OtherLetter || // (Lo)
                                uc == UnicodeCategory.LetterNumber || // (Nl)
                                name[i] == ‘_‘);
                bool idExtend = (uc == UnicodeCategory.NonSpacingMark || // (Mn)
                                 uc == UnicodeCategory.SpacingCombiningMark || // (Mc)
                                 uc == UnicodeCategory.ModifierLetter || // (Lm)
                                 uc == UnicodeCategory.DecimalDigitNumber); // (Nd)
                if (i == 0)
                {
                    if (!idStart)
                    {
                        return false;
                    }
                }
                else if (!(idStart || idExtend))
                {
                    return false;
                }
            }

            return true;
        }
    }

调用IsValidIdentifierName验证即可。

三、获取特殊文件夹路径

string myDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

Environment下定义了很多专门的路径。可以直接获取。

系统空闲时间判断&命名验证,布布扣,bubuko.com

时间: 2024-08-04 03:25:11

系统空闲时间判断&命名验证的相关文章

获取系统空闲时间

//定义结构体 internal struct LASTINPUTINFO { public uint cbSize; public uint dwTime; } //引入系统API [DllImport("User32.dll")] private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); /// <summary> /// get the system idle time /// </

C# 判断系统空闲(键盘、鼠标不操作一段时间)

利用windows API函数 GetLastInputInfo()来判断系统空闲 //添加引用 using System.Runtime.InteropServices; 1 // 创建结构体用于返回捕获时间 2 [StructLayout(LayoutKind.Sequential)] 3 struct LASTINPUTINFO 4 { 5 // 设置结构体块容量 6 [MarshalAs(UnmanagedType.U4)] 7 public int cbSize; 8 // 捕获的时间

Thinkphp的时间判断

在做项目的过程中,非常频繁地遇到时间这个问题,像时间的比较,特定时间执行某一操作,但是现在只解决了一部分问题,先说明一下时间的判断问题. 很简单,时间,不断使date(),now(),都是字符串类型的,不能直接进行比较,需要对它进行转换成int型,这里用到了一个函数——strtotime(),用于将date()转换成int. 语法格式:strtotime(time,now)      如果time是绝对时间,则now参数不起作用      如果time是相对时间,则相对应的参数则对应函数就是no

Linux系统根文件以及命名规则详解

一.Linux系统根文件详解 Linux的重要哲学思想其实就是:将程序的配置文件保存为纯文本格式. 1./boot:系统启动文件,如:内核文件,iniyrd以及gurb(bootloarder) 2./dev:目录下为设备文件,设备文件又分为块设备和字符设备: 块设备:按数据块随机访问,没有顺序. 字符设备:线性访问,按字符为单位进行. 注:其中背景为黑色,字体为***的文件,为特殊文件,"1,   0"分别为文件的主设备号和次设备号 [[email protected] ~]# ls

mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案 详细出处参考:http://www.jb51.net/article/32284.htm

MySQL 的默认设置下,当一个连接的空闲时间超过8小时后,MySQL 就会断开该连接,而 c3p0 连接池则以为该被断开的连接依然有效.在这种情况下,如果客户端代码向 c3p0 连接池请求连接的话,连接池就会把已经失效的连接返回给客户端,客户端在使用该失效连接的时候即抛出异常 解决这个问题的办法有三种: 1. 增加 MySQL 的 wait_timeout 属性的值. 修改 /etc/mysql/my.cnf文件,在 [mysqld] 节中设置: # Set a connection to w

如何在 systemd 下管理Linux系统的时间和日期

timedatectl是用来查询和修改系统时间和配置的Linux应用程序.它是 systemd 系统服务管理 的一部分,并且允许你检查和修改系统时钟的配置. 在这篇文章中,我们将涉及该小巧但十分重要应用程序的所有方面. 系统当前的时间状态 想要查看系统当前日期/时间,以及可使用不带任何参数的命令查看当前系统时间配置,可以这样: # timedatectl 输入的结果就像这样: 修改日期 想要修改系统日期,你可以简单地使用timedatectl设置时间命令,紧随命令的是YYYY-MM-DD格式的日

oracle限制一个用户空闲时间

alter system set resource_limit = true; create profile idletime limit idle_time 3; alter user outln profile idletime; SQL> alter system set resource_limit = true; 系统已更改. SQL> conn sys/sunsdl as sysdba 已连接. SQL> create profile idletime limit idle_

使用C语言获取当前系统的时间

要想使用C语言来获取当前系统的时间,办法如下: 需要提前准备的工作: 1 #include <stdio.h> 2 #include <time.h> 3 #include <stdlib.h> 4 5 int main() 6 { 7 time_t rawtime; //时间类型,由time.h中定义 8 struct tm *timeinfo; //关于时间的结构体类型,在time.h中定义 9 timeinfo = (struct tm*)malloc(sizeo

CentOS系统修改时间

新安装的CentOS7系统 本地时间显示的是其他时区的时间,我们需要调整到亚洲北京或上海时间,具体方式如下: //修正时间 #ln -sf /user/share/zoneinfo/Asia/Shanghai    /etc/localtime //设置时间 #date -s '2017-2-12 21:13:34' //讲时间写入CMOS #clock -w