系统空闲时间判断&命名验证
一、系统空闲时间判断
需要一个自动登录注销的功能,当鼠标移动和或者键盘输入的时候认为当前用户在线,否则过了设置时间就自动退出。好在前辈们留下了这样的一个类:
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 ;
}
else
{
long count = Environment.TickCount - (long)vLastInputInfo.dwTime;
long icount = count / ;
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 = ; 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 == )
{
if (!idStart)
{
return false;
}
}
else if (!(idStart || idExtend))
{
return false;
}
} return true;
}
}
调用IsValidIdentifierName验证即可。
三、获取特殊文件夹路径
string myDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Environment下定义了很多专门的路径。可以直接获取。
系统空闲时间判断&命名验证的更多相关文章
- 系统空闲时间 解决 GetLastInputInfo 负数问题
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices ...
- Winform 空闲时间(鼠标键盘无操作)
前言 Winform 在特定情况下,需要判断软件空闲时间(鼠标键盘无操作),然后在做一下一些操作. 实现 做了一个简单的例子,新建一个窗体,然后拖两个控件(Timer控件和label控件) using ...
- C# 判断系统空闲(键盘、鼠标不操作一段时间)
利用windows API函数 GetLastInputInfo()来判断系统空闲 //添加引用 using System.Runtime.InteropServices; // 创建结构体用于返回捕 ...
- js 获取系统当前时间,判断时间大小
1.获取系统当前时间 getNowTime(tempminit) { if (!tempminit) { tempminit = 0; } var date = new Date(); date.se ...
- 使用Oracle PROFILE控制会话空闲时间
客户想实现对会话空闲时间的控制,下面是做的一个例子.Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation.保留所有权利 ...
- C# 获取操作系统空闲时间
获取系统鼠标和键盘没有任何操作的空闲时间 public class CheckComputerFreeState { /// <summary> /// 创建结构体用于返回捕获时间 /// ...
- SQL Server取系统当前时间【转】
getdate //获得系统当前日期 datepart //获取日期指定部分(年月日时分表) getdate()函数:取得系统当前的日期和时间.返回值为datetime类型的. 用法:getdate( ...
- mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案
在连接字符串中 添加设置节点 ConnectionLifeTime(计量单位为 秒).超过设定的连接会话 会被杀死! Connection Lifetime, ConnectionLifeTime ...
- Thinkphp的时间判断
在做项目的过程中,非常频繁地遇到时间这个问题,像时间的比较,特定时间执行某一操作,但是现在只解决了一部分问题,先说明一下时间的判断问题. 很简单,时间,不断使date(),now(),都是字符串类型的 ...
随机推荐
- 时间“Thu Aug 14 2014 14:28:06 GMT+0800”的转换
var date = "Thu Aug 14 2014 14:28:06 GMT+0800"; var va = DateTime.ParseExact(date, "d ...
- Android中的接口回调技术
Android中的接口回调技术有很多应用的场景,最常见的:Activity(人机交互的端口)的UI界面中定义了Button,点击该Button时,执行某个逻辑. 下面参见上述执行的模型,讲述James ...
- 在SqlServer查询分析器里 访问远程数据库 进行数据查询更新等操作(openrowset)
启用Ad Hoc Distributed Queries: exec sp_configure 'show advanced options',1 reconfigure exec sp_config ...
- centOS升级python3.5
CentOS自带的版本是2.7.5 目前在看廖老师的教学,他给的新版本是3以上了,果断升级到最新的Python版本 (windows下面多线程里面有点问题没解决,所以才换到linux下继续学习) 一 ...
- 【转载】【树形DP】【数学期望】Codeforces Round #362 (Div. 2) D.Puzzles
期望计算的套路: 1.定义:算出所有测试值的和,除以测试次数. 2.定义:算出所有值出现的概率与其乘积之和. 3.用前一步的期望,加上两者的期望距离,递推出来. 题意: 一个树,dfs遍历子树的顺序是 ...
- tomee 消息持久化
http://tomee.apache.org/jms-resources-and-mdb-container.html http://activemq.apache.org/xml-configur ...
- ArcGIS百米网格自动生成
最近需要用百米网格进行空间叠加分析,首先得自动生成百米网格数据.经过查找,发现ARCgis可以自动生成网格.方法如下 2.创建格网(Creat Fishnet).需要用到ArcGIS的ArcToolb ...
- 每天记一些php函数,jQuery函数和linux命令(二)
简介:学习完了php和jQuery之后,对函数的记忆不到位,导致很多函数没记住,所以为了促进自己的记忆,每天花一点时间来写这个博客. 时间:2016-12-19 地点:太原 天气:晴 一.p ...
- 查看当前正在运行的activity
找到sdk的安装路径,比如我的是 D:\prostu\Android\android-sdk\tools该路径下的: hierarchyviewer.bat 双击,可以用此工具查看设备跑的是当前的哪个 ...
- 安卓Notification的setLatestEventInfo is undefined出错不存在的解决
用最新版的SDK,在做状态栏通知时,使用了Notification的setLatestEventInfo(),结果提示: The method setLatestEventInfo(Context, ...