C # Read the TXT document of notepad to the able.

Source: Internet
Author: User

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

 

Related Article

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.