System idle time judgment & name verification

Source: Internet
Author: User

1. system idle time judgment

An automatic login and logout function is required. When you move the mouse or enter the keyboard, the user is deemed to be online. Otherwise, the user exits automatically after the set time. Fortunately, the predecessors left such a class:

Mousekeyboardoperate:

Using system; using system. runtime. interopservices; namespace SCADA. RTDB. framework. helpers {// <summary> // class mousekeyboardoperate // </Summary> public class mousekeyboardoperate {// <summary> // create a struct to return the capture time/ // </Summary> layoutkind. sequential is used to force members to be laid out in the order in which they appear. [Structlayout (layoutkind. sequential)] struct lastinputinfo {// <summary> /// set the capacity of the struct block /// </Summary> the delealas attribute instructs you how to mail data between hosted and unmanaged code. [Financialas (unmanagedtype. u4)] public int cbsize; // <summary> // capture time /// </Summary> [financialas (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> // get the time when no operation is performed on the keyboard or mouse /// </Summary> /// <RET Urns> the time interval from the last time the user used the system, in seconds </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 ;}}}}

Call mousekeyboardoperate. getlastinputtime () to obtain the number of seconds currently idle. If you move the mouse or enter this value on the keyboard, the value will change to 0 immediately.

Ii. Name verification.

When the name set of a model object, we need to verify its validity. It cannot contain special characters or begin with a number.

 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;        }    }

Call isvalididentifiername for verification.

3. Get the special folder path

String mydesktop = environment. getfolderpath (environment. specialfolder. desktop );

Environment defines many specialized paths. Can be obtained directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.