一、系统空闲时间判断

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

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下定义了很多专门的路径。可以直接获取。

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

  1. 系统空闲时间 解决 GetLastInputInfo 负数问题

    using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices ...

  2. Winform 空闲时间(鼠标键盘无操作)

    前言 Winform 在特定情况下,需要判断软件空闲时间(鼠标键盘无操作),然后在做一下一些操作. 实现 做了一个简单的例子,新建一个窗体,然后拖两个控件(Timer控件和label控件) using ...

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

    利用windows API函数 GetLastInputInfo()来判断系统空闲 //添加引用 using System.Runtime.InteropServices; // 创建结构体用于返回捕 ...

  4. js 获取系统当前时间,判断时间大小

    1.获取系统当前时间 getNowTime(tempminit) { if (!tempminit) { tempminit = 0; } var date = new Date(); date.se ...

  5. 使用Oracle PROFILE控制会话空闲时间

    客户想实现对会话空闲时间的控制,下面是做的一个例子.Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation.保留所有权利 ...

  6. C# 获取操作系统空闲时间

    获取系统鼠标和键盘没有任何操作的空闲时间 public class CheckComputerFreeState { /// <summary> /// 创建结构体用于返回捕获时间 /// ...

  7. SQL Server取系统当前时间【转】

    getdate //获得系统当前日期 datepart //获取日期指定部分(年月日时分表) getdate()函数:取得系统当前的日期和时间.返回值为datetime类型的. 用法:getdate( ...

  8. mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案

    在连接字符串中  添加设置节点 ConnectionLifeTime(计量单位为 秒).超过设定的连接会话 会被杀死! Connection Lifetime, ConnectionLifeTime ...

  9. Thinkphp的时间判断

    在做项目的过程中,非常频繁地遇到时间这个问题,像时间的比较,特定时间执行某一操作,但是现在只解决了一部分问题,先说明一下时间的判断问题. 很简单,时间,不断使date(),now(),都是字符串类型的 ...

随机推荐

  1. Spring知识点总结大全(1)

    1.Spring的分层结构 1.Presentation layer(表示层) (1) 表示逻辑(生成界面代码) (2) 接收请求 (3) 处理业务层抛出的异常 (4) 负责规则验证(数据格式,数据非 ...

  2. 了解Hadoop和大数据

    1. 场景: 现在人产生数据越来越快,机器则更快,所以需要另外的一种处理数据的方法.   硬盘容量增加,但是性能没跟上,解决办法是将数据分到多块硬盘,然后同时读取. 问题:     硬件问题 -- 复 ...

  3. 多个网站使用不同的SSH密钥登陆(zz)

    多个网站使用不同的SSH密钥登陆   1.创建不同的SSH密钥, -t指定加密方法,RSA或DSA:-C注释:-f指定文件名   www.2cto.com   ssh-keygen -t dsa -C ...

  4. kiosk-mode,免密码登陆, sideload Windows Store apps 等

    MVVM带来的性能问题及其解决方案  MVVM 和语言性能提示:https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/mt628050. ...

  5. HDU Game Theory

    5795 || 3032 把x个石子的堆分成非空两(i, j)或三堆(i, j, k)的操作->(sg[i] ^ sg[j])或(sg[i] ^ sg[j] ^ sg[k])是x的后继 #def ...

  6. sharebutton

    <h1>Share Buttons</h1> <!-- Twitter --> <a href="http://twitter.com/share? ...

  7. win7下matplotlib安装(64位)

    前段时间爬了一些数据,想着以后要将数据的分析结果什么的展示出来,就想着下个MATLAB,某天在微信上的一篇文章发现matplotlib库,是用于Python的一个不错的图形化库,就想着装上耍耍.不过安 ...

  8. lvs-keepalived故障记录

    上图中1与2.3不同,可能会导致端口不同,尽量配置1.2.3相同

  9. myBatis+SpringMVC+Maven整合

    一.先创建表结构 二.使用generator通过表结构自动生成model和dao.mapper 使用步骤: 1.解压generator.rar文件 2.文件中的generator.xml文件需要进行修 ...

  10. android开机过程简单描述

    1 开机引导bootloader,相当于电脑开机启动bios 2 引导过后可以进入三种模式:fastboot, recovery, linux kernel.前两种跟版本升级相关,正常开机进入linu ...