C # Write a TXT file

Source: Internet
Author: User
Using  System. IO;  Using  System. diagnostics;  Namespace  Webapplication1 {  Public   Class  Filetext {  Public   Static   String Fileaddress = "  C: \ error.txt  "  ; ///   <Summary>          ///  Record the wrong class name and method name and error message  ///   </Summary>          ///   <Param name = "classname">  Error Class Name  </Param>          ///   <Param name = "ex">  Error instance  </Param>          Public   Static  Void  Record (exception ex ){  Try  {Filestream FS = Null  ;  If (! File. exists (fileaddress) {FS = New  Filestream (fileaddress, filemode. Create );}  Else  {FS =New  Filestream (fileaddress, filemode. append);} stacktrace St = New Stacktrace ( True  );  String Transfermethod = ST. getframe ( 1  ). Getmethod (). Name. tostring (); streamwriter SW = New  Streamwriter (FS); Sw. writeline (  "  ------------------------------ "  ); Sw. writeline (  "  Date:  " + Datetime. Now. tostring ( "  G  "  ); Sw. writeline (  "  Method for calling error records:  " + Transfermethod); Sw. writeline (  "  Method for triggering the error: " + Ex. targetsite. tostring (); Sw. writeline (  "  Error message:  " + Ex. Message. tostring (); Sw. writeline (  "  ------------------------------  "  ); Sw. Close ();}  Catch  (Ioexception IOE ){}}  Public  Static   Void Writestring ( String  Str) {filestream FS = Null  ;  If (! File. exists (fileaddress) {FS = New  Filestream (fileaddress, filemode. Create );}  Else  {FS = New Filestream (fileaddress, filemode. append);} stacktrace St = New Stacktrace ( True  );  String Transfermethod = ST. getframe ( 1  ). Getmethod (). Name. tostring (); streamwriter SW = New  Streamwriter (FS); Sw. writeline (  "  ------------------------------  " ); Sw. writeline (  "  Date:  " + Datetime. Now. tostring ( "  G  "  ); Sw. writeline (  "  Error message:  " + Str); Sw. writeline (  "  ------------------------------  " ); Sw. Close ();}}} 

 

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.