Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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