Several C # Programming Tips (2)
Published: 2003-5-11
First, determine if the file or folder exists using system.io.file, check if a file is very simple: BOOL EXIST = System.IO.File.exists (FileName); if you need to judge whether the directory (folder) is existing, You can use system.io.directory :bool exists (FolderName); Second, use the delegate type design Custom Event in C # programming, in addition to Method and Property, any class can have its own event ( Event). The steps to define and use Custom Events are as follows: (1) Define a delegate type outside of the Class to determine the interface of the event program (2) inside the class, declare a public event variable, the type is the Delegate defined by the previous step. Type (3) Somewhere in a Method or Property, trigger event (4) CLIENT Using = Operator Specifies an Event Handle Example: / / Define Delegate Type, Constrained Event Programs Parameters PUBLIC DELEGATE VOID MyEventHandler (Object) sender, long lineNumber); public class DataImports {// define a new event NewLineReadpublic event MyEventHandler NewLineRead; public void ImportData () {long i = 0; // event parameters while () {i ; // trigger event if (NewLineRead =! NULL) NEWLINEREAD (this, i); // ...} // ...} // ...} // The following is a client code private void callmethod () {// declared the Class variable, no WitHeventSprivate DataImports _da = NULL; // Specify event handler _da.newlineRead = new myeventhandler (this.da_enternewline); // Call the Class method, trigger the event _da.importData ();} // Event handler private void da_enternewline Object sender, long linenumber) {// ...} III, IP and hostname resolution Use System.net to implement IP parsing features similar to the ping command line, such as resolving the host name to IP or in turn: Private string gethostnamebyip (IPHOSTENTRY HOSTINFO = DNS.GETOSTBYADDRESS (IPADDRESS); Return Hostinfo.hostname;} private string getipbyhostname (String Hostn AME) {system.net.iphostentry Hostinfo = DNS.GETHOSTBYNAME (HostName); return hostinfo.addresslist [0] .tostring ();