The usual way to feel good about your packaging-not to be continued

Source: Internet
Author: User

Write to create TXT file

/// <summary>    ///Generating Files/// </summary>    /// <param name= "Content" >File Contents</param>     Public voidCreatepage (stringContentstringfilename) {        Try        {            if(! System.IO.Directory.Exists (filename. Substring (0, filename. LastIndexOf ('/')) {System.IO.Directory.CreateDirectory (filename). Substring (0, filename. LastIndexOf ('/'))); } FileStream FS=NewFileStream (filename, filemode.create); StreamWriter SW=NewStreamWriter (FS, System.Text.Encoding.GetEncoding ("GB2312")); Sw.            WriteLine (content); Sw.            Flush (); Sw.            Close (); BOOLresult =< span="">if (Result = = false) {  // send file failed HttpContext.Current.Response.Wr            ITE (filename + " send failed "); }}  catch  (Exception ex) {HttpContext.Current.Response.Write (Filena Me + ex.        ToString ()); }    }

If a TXT file exists to write to the file

 /// <summary>    ///Write File/// </summary>    /// <param name= "Dizhi" >Write Address</param>    /// <param name= "Content" >Write Content</param>     Public stringWriteToFile (stringDizhi,stringContentBOOLIsfugai) {        stringMessage =string.        Empty; Try{System.IO.FileInfo file=NewSystem.IO.FileInfo (Dizhi); //exclusive way, because a file can only be written by one process.System.IO.StreamWriter Writer1 =NULL; Writer1=NewSystem.IO.StreamWriter (file. FullName, Isfugai);//The file does not exist and is createdWriter1.            WriteLine (content); Writer1.        Close (); }        Catch{message="someone is syncing the data later, please sync"; }        returnmessage; }

Write TXT file

   /// <summary>    ///Get token/// </summary>    /// <returns></returns>     Public stringGetjsapi_tiket (stringtoken) {FileStream FS1=NewFileStream ("D:/webwz/m/wxtest/jsapi_tiket.txt", FileMode.Open); StreamReader SR=NewStreamReader (FS1, Encoding.GetEncoding ("GBK")); stringAcctoken =Sr.        ReadToEnd (); Sr.        Close (); FS1.        Close (); Getqianming at= jsondeserialize<getqianming>(Acctoken); if(DateTime.Now >convert.todatetime (at.expires_in)) {            //regain the Jsapi_tiket token after the current time is greater than the expiration Time//gets the JSON data returned to the            stringTicket = GetPage ("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+token+"&type=jsapi"); Getqianming Jstiket= jsondeserialize<getqianming>(ticket); stringJstik =Jstiket.ticket; //writes the returned data to a file//{"Errcode": 0, "errmsg": "OK", "Ticket": " bxldikrxvbtpdhsm05e5u6thi9cropfeaorrzm-klx3nmkjvnp0u-oa3dl920a6asebxm2dztrcakxls5yoikg "," expires_in ": 7200} //E:/work1/wxtest/jsapi_tiket.txtWriteToFile ("D:/webwz/m/wxtest/jsapi_tiket.txt","{\ "errcode\":"+ Jstiket.errcode +", \ "errmsg\": \ ""+ jstiket.errmsg +"\ ", \" ticket\ ": \""+ Jstik +"\ ", \" expires_in\ ": \""+ DateTime.Now.AddSeconds (Convert.ToInt32 (jstiket.expires_in)- -) +"\"}",false); returnJstik; }        Else        { //continue using the Accesstoken token obtained from the document without expiring            returnAt.ticket; }    }

JSON-deserialized

  /// <summary>    ///JSON format Conversion/// </summary>    /// <typeparam name= "T" ></typeparam>    /// <param name= "jsonstring" ></param>    /// <returns></returns>     PublicT jsondeserialize<t> (stringjsonstring) {System.Runtime.Serialization.Json.DataContractJsonSerializer Ser=NewSystem.Runtime.Serialization.Json.DataContractJsonSerializer (typeof(T)); System.IO.MemoryStream Ms=NewSystem.IO.MemoryStream (System.Text.Encoding.UTF8.GetBytes (jsonstring)); T obj=(T) ser.        ReadObject (MS); returnobj; }

The usual way to feel good about your packaging-not to be continued

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.