Internal reform of the company, to make adjustments to the attendance, the need to achieve office automation, in OA for work attendance; As a programmer, how to realize automated attendance application?
The requirements are as follows:
- Can set attendance address, user information, commuting time;
- According to the set time of commuting, automatic attendance is realized.
The specific implementation steps are as follows:
- Form interface add time and attendance address, user information, work hours, WebBrowser control;
- Change the WebBrowser address to the attendance address
- After the WebBrowser load is completed, enter the user, password information, login into the system.
- Use the thread blocking form, estimate log in to enter the system time, and then enter the attendance menu for attendance
The code is implemented as follows:
Public Partial classForm1:form {/// <summary> ///The counter determines whether the Web page is loaded/// </summary> inti =0; //Log in Timer intj =0; //Work Timer intm =0; PublicForm1 () {InitializeComponent (); Txturl.enabled=false; Txtuser.enabled=false; Txtpwd.enabled=false; Tdtstarttime.enabled=false; Tdtendtime.enabled=false; WM=getwork (); Webbrowser.url=Wm. URL; Webbrowser.documentcompleted+=NewWebbrowserdocumentcompletedeventhandler (webbw_documentcompleted); Webbrowser.navigated+=NewWebbrowsernavigatedeventhandler (webbrowser_navigated); } /// <summary> ///Use the counter to determine whether the interface is loaded/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> voidWebbrowser_navigated (Objectsender, WebBrowserNavigatedEventArgs e) {i++; } voidWebbw_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e) {i--; if(0==i) {if(J <1) {lognin (); J++; } if(M <1&& J >0) {HTMLDocument Htmldoc=webbrowser.document; HtmlElement Txtmenucodequick= htmldoc.all["Menucodequick"]; if(Txtmenucodequick! =NULL) {txtmenucodequick.focus (); Txtmenucodequick.setattribute ("value","hr43b"); Sendkeys.sendwait ("{ENTER}"); M++; } } } } /// <summary> ///Login/// </summary> voidLognin () {HTMLDocument Htmldoc=webbrowser.document; HtmlElement Txtuser= htmldoc.all["Txtusercodel"]; Txtuser.focus (); if(Txtuser! =NULL) {Txtuser.setattribute ("value", Wm. User); } sendkeys.sendwait (Keys.Tab.ToString ()); HtmlElement Txtpassword= Htmldoc.getelementbyid ("txtpasswordl"); if(Txtpassword! =NULL) {Txtpassword.setattribute ("value", Wm. Password); } thread.sleep ( +); HtmlElement Btndl= Htmldoc.getelementbyid ("Ctl00$ctl00$c1$cb$btnlogin"); Btndl.invokemember ("Click"); Thread.Sleep ( +); } PrivateWorkmanger getwork () {Uri u=NewUri (Txturl.text); Uml URL=u; Uml User=Txtuser.text; Uml Password=Txtpwd.text; Uml Strattime=Tdtstarttime.time; Uml EndTime=Tdtendtime.time; returnWM; } //opens a page based on the specified URL address voidopenie (Uri url) {webbrowser.url=URL; } PrivateWorkmanger WM =NewWorkmanger (); voidendwork () {HTMLDocument Htmldoc=webbrowser.document; HtmlElement Rtbtext= htmldoc.all["ctl00_ctl00_c1_h1_wsdbasictoolbar1"]; StringBuilder SB=NewStringBuilder (); foreach(HtmlElement iteminchhtmldoc.all) {sb. Appendline (item. ID); } file.writealltext (@"D:\html.txt", sb. ToString (), Encoding.UTF8); } } /// <summary> ///attendance System class/// </summary> Public classWorkmanger {/// <summary> ///Address/// </summary> PublicUri URL {Get;Set; } /// <summary> ///User name/// </summary> Public stringUser {Get;Set; } /// <summary> ///Password/// </summary> Public stringPassword {Get;Set; } /// <summary> ///Working Hours/// </summary> PublicDatetime? Strattime {Get;Set; } /// <summary> ///Time off/// </summary> PublicDatetime? EndTime {Get;Set; } }
Using WebBrowser to implement the automatic login function