Skip to content

Instantly share code, notes, and snippets.

@monakaice
Last active March 9, 2017 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monakaice/62ef6c4d79ef91749d0508b39e263c9e to your computer and use it in GitHub Desktop.
Save monakaice/62ef6c4d79ef91749d0508b39e263c9e to your computer and use it in GitHub Desktop.
tail -fコマンドっぽいもの
using System;
using System.IO;
using System.Linq;
using System.Threading;
namespace TailFile
{
class Program
{
static void Main( string[] args )
{
try
{
validateArgs( args );
}
catch ( ArgumentException ex )
{
Console.WriteLine( ex.Message );
return;
}
try
{
using ( FileStream stream = new FileStream( args[ 0 ], FileMode.Open, FileAccess.Read, FileShare.ReadWrite ) )
using ( StreamReader reader = new StreamReader( stream ) )
{
while ( true )
{
string lineString = reader.ReadLine();
if ( lineString == null )
{
Thread.Sleep( 300 );
}
else
{
Console.WriteLine( lineString );
}
}
}
}
catch ( Exception ex )
{
Console.WriteLine( ex.Message );
}
}
private static void validateArgs( string[] args )
{
if ( args.Count() == 0 )
{
throw new ArgumentException( "第1引数にファイルを指定して下さい" );
}
if ( !File.Exists( args[ 0 ] ) )
{
throw new ArgumentException( "第1引数にファイルを指定して下さい" );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment