#region Ping returns True to indicate that a successful public bool Ping (string remotehost) {bool Flag = false can be connected; Process proc = new process (); try {Proc. Startinfo.filename = "cmd.exe"; Proc. Startinfo.useshellexecute = false; Proc. Startinfo.redirectstandardinput = true; Proc. Startinfo.redirectstandardoutput = true; Proc. Startinfo.redirectstandarderror = true; Proc. Startinfo.createnowindow = true; Proc. Start (); String dosline = @ "Ping-n 1" + remotehost; Proc. Standardinput.writeline (Dosline); Proc. Standardinput.writeline ("Exit"); while (!proc. hasexited) {Proc. WaitForExit (500); } string pingresult = Proc. Standardoutput.readtoend (). Replace ("Request timed out", "unable to access the target host"); if (Pingresult.indexof ("Unreachable target host") ==-1) {Flag = true; } proc. Standardoutput.close (); } catch (Exception ex) {} finally {Proc. Close (); Proc. Dispose (); } return Flag; } #endregion
#region Connect LAN Returns True to indicate that a successful public bool connect can be connected (string remotehost, String userName, String passwo RD) {bool Flag = false; Process proc = new process (); try {Proc. Startinfo.filename = "cmd.exe"; Proc. Startinfo.useshellexecute = false; Proc. Startinfo.redirectstandardinput = true; Proc. Startinfo.redirectstandardoutput = true; Proc. Startinfo.redirectstandarderror = true; Proc. Startinfo.createnowindow = true; Proc. Start (); Proc. Standardinput.writeline ("net use */del/y"); Disconnect all connections in string dosline = @ "net use \ \" + RemoteHost + "" + PassWord + "+"/user: "+ userName +" > NUL "; Proc. Standardinput.writeline (Dosline); Proc. Standardinput.writeline ("Exit"); while (!proc. hasexited) {Proc. WaitForExit (1000); } string errormsg = Proc. Standarderror.readtoend (); Proc. Standarderror.close (); if (String.IsNullOrEmpty (errormsg)) {Flag = true; }} catch (Exception ex) {} finally {Proc. Close (); Proc. Dispose (); } return Flag; } #endregion
C # user name password access LAN share