. NET quick access to the last text of the network text file-Small application

Source: Internet
Author: User
Scene

Now the company's test environment some text log does not allow access to provide a network of HTTP server for direct access, this file is large, general 10MB a file, not the same LAN, speed limit 200K, to wait for a long time, access is very slow. . NET code requests the latest content of a text file (similar to tail file)

Namespace ConsoleApplication3 {class Program {static void Main (string[] args) {WHI
                Le (True) {//var filelength = 0L;
                var sposstion = 0;
                int completedlength = 0; var FilePath = configurationmanager.appsettings["Txtfilepath"].
                ToString ();

                Console.WriteLine ("Network TXT file path: {0}", FilePath);
                var lengthrequest = (HttpWebRequest) httpwebrequest.create (FilePath); Lengthrequest.method = "Head";//Get file headers only, do not get content, speed fast var contentlength = ((HttpWebResponse) lengthrequest.getre Sponse ()).
                ContentLength;
                try {lengthrequest.abort (); The catch (Exception ex) {Console.WriteLine (ex).
                message); var getlastlength = Convert.ToInt32 (configurationmanager.appsettings["Getlastlength"). ToString ());
                HttpWebRequest myrequest = (HttpWebRequest) httpwebrequest.create (FilePath); if (ContentLength > Getlastlength) {myrequest.addrange ((int) (Contentlength-getla  STLENGTH);//Set range value}//request to server, get server response data stream HttpWebResponse WebResponse =
                (HttpWebResponse) myrequest.getresponse (); Filelength = webresponse.contentlength + sposstion;//File size = downloaded + server returned Stream MyStream = Webresponse.get
                Responsestream ();

                byte[] btcontent = new Byte[getlastlength];
                Save ();//Saving Download Information if ((Completedlength = Mystream.read (btcontent, 0, getlastlength)) > 0)
                    {Console.WriteLine (Encoding.UTF8.GetString (btcontent));
                    Fstream.write (btcontent, 0, completedlength);
                    Downloadedlength + = Completedlength; if (Bestop = = TRUE) break;
                else {Console.WriteLine ("No Content");
                }//fstream.close ();
                Mystream.close ();
                Console.WriteLine ("Last Refresh Time:" +datetime.now.tostring ("Yyyy-mm-dd hh:m:ss.fff"));
                Console.WriteLine ("\ r \ n---------Press any key to refresh----------");
                Console.readkey ();
            Console.clear ();
 }

        }
    }
}

Config file configuration:

  <appSettings>
    <add key= "Txtfilepath" value= "Http://www.text.com/Info/Info.log"/> <add key=
    "Getlastlength" value= "2048"/>
  </appSettings>
run the effect, less than 1 seconds out, refresh fast

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.