Sometimes we only need to use a few simple data items. There is no need to create a separate table in the database to store the data and connect to the database.
For exampleProgramYou only need to regularly send emails to several people, And the email addresses of these people can be put in the TXT document and then read out.
Put the data read from the TXT file into the datatable to return the data, so that the main program can traverse the datatable.
Previously I thought of putting the data into an array, but the length of the data is not fixed, so there is uncertainty in the definition of the array. The best choice is datatable,CodeAs follows:
Notepad path:
// Add reference
Using system. Data. sqlclient;
Using system. IO;
//Notepad path (preferably relative path)Public Static StringStrnocheckgroup =@".. \ Mailaddress \ mailaddress.txt";
// Readtxt Public Static System. Data. datatable readtxt ( String Dirtxt) {streamreader objreader = New Streamreader (dirtxt); system. Data. datatable dt = New System. Data. datatable (); DT. Columns. Add ( " DN " , System. type. GetType ( " System. String " )); String Sline = "" ; While (Sline! = Null ) {Sline = Objreader. Readline (); If (Sline! = Null &&! Sline. Equals ( "" ) {Datarow Dr = DT. newrow (); Dr [ " DN " ] = Sline; DT. Rows. Add (DR) ;}} objreader. Close (); Return DT ;} // Writetxt Public Static Void Writetxt (String Dirtxt, system. Data. datatable DT) {filestream FS = New Filestream (dirtxt, filemode. Create); streamwriter SW = New Streamwriter (FS ); // Start writing For ( Int I = 0 ; I <DT. Rows. Count; I ++ ) {Sw. Write (Dt. Rows [I] [ 0 ]. Tostring () + " " + Dt. Rows [I] [ 1 ]. Tostring () + " " + Dt. Rows [I] [ 2 ]. Tostring () + " " + Dt. Rows [I] [ 3 ]. Tostring () + " " + Dt. Rows [I] [ 4 ]. Tostring () + " \ R \ n " );} // Clear the buffer Sw. Flush (); // Close stream Sw. Close (); FS. Close ();} // Writetxt Public Static Void Writestringtotxt ( String Dirtxt,String Str) {filestream FS = New Filestream (dirtxt, filemode. Create); streamwriter SW = New Streamwriter (FS ); // Start writing Sw. Write (STR + " \ R \ n " ); // Clear the buffer Sw. Flush (); // Close stream Sw. Close (); FS. Close ();}
Read the string field and store it in list <string> to obtain the IP address of the computer (multiple NICS ):
Public Static List < String > Iplist = New List < String > (); Public Static Void IPaddress (){ Try { String Localip = "" ; System. net. IPaddress [] Addresslist = DNS. gethostentry (DNS. gethostname (). Addresslist; Foreach (IPaddress IP In Addresslist ){ If (IP. addressfamily =Addressfamily. InterNetwork) {localip = IP. tostring (); iplist. Add (localip );}}} Catch (Exception) {iplist. Add ( " 0.0.0.0 " );}} Public Static Void Getipaddress () {IPaddress (); For ( Int I = 0 ; I <iplist. Count; I ++ ) {Console. writeline (iplist [I]) ;}}