Last active
March 9, 2017 21:36
-
-
Save monakaice/62ef6c4d79ef91749d0508b39e263c9e to your computer and use it in GitHub Desktop.
tail -fコマンドっぽいもの
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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