Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1087789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:59:06+00:00 2026-05-16T22:59:06+00:00

I want to read file continuously like GNU tail with -f param. I need

  • 0

I want to read file continuously like GNU tail with “-f” param. I need it to live-read log file.
What is the right way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:59:07+00:00Added an answer on May 16, 2026 at 10:59 pm

    You want to open a FileStream in binary mode. Periodically, seek to the end of the file minus 1024 bytes (or whatever), then read to the end and output. That’s how tail -f works.

    Answers to your questions:

    Binary because it’s difficult to randomly access the file if you’re reading it as text. You have to do the binary-to-text conversion yourself, but it’s not difficult. (See below)

    1024 bytes because it’s a nice convenient number, and should handle 10 or 15 lines of text. Usually.

    Here’s an example of opening the file, reading the last 1024 bytes, and converting it to text:

    static void ReadTail(string filename)
    {
        using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            // Seek 1024 bytes from the end of the file
            fs.Seek(-1024, SeekOrigin.End);
            // read 1024 bytes
            byte[] bytes = new byte[1024];
            fs.Read(bytes, 0, 1024);
            // Convert bytes to string
            string s = Encoding.Default.GetString(bytes);
            // or string s = Encoding.UTF8.GetString(bytes);
            // and output to console
            Console.WriteLine(s);
        }
    }
    

    Note that you must open with FileShare.ReadWrite, since you’re trying to read a file that’s currently open for writing by another process.

    Also note that I used Encoding.Default, which in US/English and for most Western European languages will be an 8-bit character encoding. If the file is written in some other encoding (like UTF-8 or other Unicode encoding), It’s possible that the bytes won’t convert correctly to characters. You’ll have to handle that by determining the encoding if you think this will be a problem. Search Stack overflow for info about determining a file’s text encoding.

    If you want to do this periodically (every 15 seconds, for example), you can set up a timer that calls the ReadTail method as often as you want. You could optimize things a bit by opening the file only once at the start of the program. That’s up to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In PHP, I want to read a file into a variable and process the
I want to read an xml file, apply a transform, then write to another
I want to read line n1->n2 from file foo.c into the current buffer. I
I have a file that I want to read in using the File::Slurp module
I have an INI file that I want to read the line DeviceName=PHJ01444-MC35 from
I added a text file to a testapp's solution and I want to read
I want to be able to read from an unsorted source text file (one
I want to do some basic filtering on a file. Read it, do processing,
Hi programmers, I want read line by line a Unicode (UTF-8) text file created
i want to read file in dialog can any one guide me how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.