一、系统空闲时间判断

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

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. Ubuntu Server安装R和Rstudio(zz)

    Ubuntu Server安装R和Rstudio 发表于 技术天堂 2014-03-15 21:03 字数: 534 阅读量: 205 R是一个在科研领域很常用的工具,经常用R的年轻人或者经常上统计之 ...

  2. Android自定义View之CircleView

    Android自定义View之CircleView 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/5999 ...

  3. 判断Window在哪个屏幕

    最近在做窗口最大化时需要一个功能,如果是多个显示器的话,需要在当前显示器最大化,由于是根据屏幕长宽进行设置Window大小,没有使用WindowState.Maximized,window.Left不 ...

  4. Jquery插件开发精品教程

    最开始接触jquery对他提供的各种插件总是十分有兴趣,但是总是不理解为什么这样写,从网络上查询了很久终于找到这篇文章,讲解的很详细,分享给大家. 要说jQuery 最成功的地方,我认为是它的可扩展性 ...

  5. 1245 - Harmonic Number (II)---LightOJ1245

    http://lightoj.com/volume_showproblem.php?problem=1245 题目大意:一个数n除以1到n之和 分析:暴力肯定不行,我们可以先求1~sqrt(n)之间的 ...

  6. 【日常小问题】windows系统操作技巧

    如何将文件夹视图设置为“详细信息” @2015.3.30 这段时间刚换了电脑,资源管理器视图模式为“大图标”,很不符合俺的操作习惯.修改之—— 1.打开一个文件夹,设置为详细信息显示. 2.点击工具- ...

  7. lambda 表达式 比较时间大小

    data.Orderdetails.Where(r => r.orderstatus == id && DateTime.Compare((DateTime)r.OrderTim ...

  8. 在SqlServer查询分析器里 访问远程数据库 进行数据查询更新等操作(openrowset)

    启用Ad Hoc Distributed Queries: exec sp_configure 'show advanced options',1 reconfigure exec sp_config ...

  9. configure: error: no acceptable C compiler found in $PAT 的解决方案

    configure: error: no acceptable C compiler found in $PATH See `config.log' for more details.你的机器里没有安 ...

  10. IAR FOR ARM的安装及破解

    本博文主要是介绍如何安装以及破解IAR FOR ARM . 1.下载IAR FOR ARM以及注册机 IAR FOR ARM下载:http://pan.baidu.com/s/1i5t1qF7 注册机 ...