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


    一、系统空闲时间判断   

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

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

  • 相关阅读:
    fibnacci数列递归实现
    求最大公约数伪代码
    第五周学习总结
    第四周学习总结
    我的黑客偶像
    BASE64编码
    第三周学习总结
    第二周学习总结
    Python gui
    SSH实践
  • 原文地址:https://www.cnblogs.com/stoneniqiu/p/3853461.html
Copyright © 2020-2023  润新知