work24 通过本文主要向大家介绍了c#文本框只输入数字,c#文本框只能输入数字,c#输入语句,c#输入,c#密码输入框等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
本文实例讲述了C#从文件或标准输入设备读取指定行的方法。分享给大家供大家参考。具体如下:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace RobvanderWoude
{
class ReadLine
{
static int Main( string[] args )
{
#region Command Line Parsing
string filename = string.Empty;
int linestart = 1;
int lineend = 2;
bool concat = false;
bool addspaces = false;
string concatchar = string.Empty;
bool skipempty = false;
bool trimlines = false;
bool numlines = false;
bool redirected;
bool set_c = false;
bool set_l = false;
bool set_s = false;
bool set_t = false;
bool set_input = false;
if ( ConsoleEx.InputRedirected )
{
set_input = true;
redirected = true;
}
else
{
if ( args.Length == 0 )
{
return WriteError( );
}
redirected = false;
}
foreach ( string arg in args )
{
if ( arg[0] == '/' )
{
try
{
switch ( arg.ToUpper( )[1] )
{
case '?':
return WriteError( );
case 'C':
if ( arg.ToUpper( ) != "/C" && arg.ToUpper( ) != "/CS" )
{
return WriteError( "Invalid command line switch " + arg );
}
concat = true;
if ( arg.ToUpper( ) == "/CS" )
{
addspaces = true;
}
if ( set_c )
{
return WriteError( "Duplicate command line argument /C*" );
}
set_c = true;
break;
case 'L':
if(arg.ToUpper( ).StartsWith( "/L:" ) && arg.Length > 3)
{
if ( arg[2] == ':' )
{
string linessel = arg.Substring( 3 );
string pattern = @"^(\-?\d+)$";
Match match = Regex.Match( linessel, pattern );
if ( match.Success )
{
linestart = Convert.ToInt32( match.Groups[1].Value );
lineend = linestart + 1;
}
else
{
pattern = @"^(\-?\d+)\.\.(\-?\d+)$";
match = Regex.Match( linessel, pattern );
if ( match.Success )
{
linestart = Convert.ToInt32( match.Groups[1].Value );
lineend = Convert.ToInt32( match.Groups[2].Value ) + 1;
}
else
{
pattern = @"^(\-?\d+),(\-?\d+)$";
match = Regex.Match( linessel, pattern );
if ( match.Success )
{
// numlines is true if the second number
//specifies the number of lines instead of a line number
numlines = true;
linestart = Convert.ToInt32( match.Groups[1].Value );
lineend = Convert.ToInt32( match.Groups[2].Value );
if ( lineend < 1 )
{
return WriteError( "Invalid number of lines (" + lineend.ToString( ) + "), must be 1 or higher" );
}
}
}
}
}
else
{
return WriteError( "Invalid command line switch " + arg );
}
}
else
{
return WriteError( "Invalid command line switch " + arg );
}
if ( set_l )
{
return WriteError( "Duplicate command line argument /L" );
}
set_l = true;
break;
case 'S':
if ( arg.ToUpper( ) != "/SE" )
{
return WriteError( "Invalid command line switch " + arg );
}
skipempty = true;
if ( set_s )
{
return WriteError( "Duplicate command line argument /SE" );
}
set_s = true;
break;
case 'T':
if ( arg.ToUpper( ) != "/T" )
{
return WriteError( "Invalid command line switch " + arg );
}
trimlines = true;
if ( set_t )
{
return WriteError( "Duplicate command line argument /T" );
}
set_t = true;
break;
default:
return WriteError( "Invalid command line switch " + arg );
}
}
catch
{
return WriteError( "Invalid command line switch " + arg );
}
}
else
{
if ( set_input )
{
return WriteError( "Multiple inputs specified (file + redirection or multiple files)" );
}
if ( redirected )
{
return WriteError( "Do not specify a file name when using redirected input" );
}
else
{
filename = arg;
}
}
}
#endregion
try
{
int count = 0;
bool output = false;
string[] lines;
List<string> alllines = new List<string>( );
if ( redirected )
{
// Read standard input and store the lines in a list
int peek = 0;
do
{
alllines.Add( Console.In.ReadLine( ) );
} while ( peek != -1 );
// Convert the list to an array
lines = alllines.ToArray( );
}
else
{
// Read the file and store the lines in a list
lines = File.ReadAllLines( filename );
}
// Check if negative numbers were used, and if so,
//calculate the resulting line numbers
if ( linestart < 0 )
{
linestart += lines.Length + 1;
}
if ( lineend < 0 )
{
lineend += lines.Length + 1;
}
if ( numlines )
{
lineend += linestart;
}
// Iterate through the array of lines and display
//the ones matching the command line switches
foreach ( string line in lines )
{
string myline = line;
if ( trimlines )
{
myline = myline.Trim( );
}
bool skip = skipempty && ( myline.Trim( ) == string.Empty );
if ( !skip )
{
count += 1;
if ( count >= linestart && count < lineend )
{
if ( concat )
{
Console.Write( "{0}{1}", concatchar, myline );
}
else
{
Console.WriteLine( myline );
}
if ( addspaces )
{
concatchar = " ";
}
}
}
}
}
catch ( Exception e )
{
return WriteError( e.Message );
}
return 0;
}
#region Redirection Detection
public static class ConsoleEx
{
public static bool OutputRedirected
{
get
{
return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdout ) );
}
}
public static bool InputRedirected
{
get
{
return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stdin ) );
}
}
public static bool ErrorRedirected
{
get
{
return FileType.Char != GetFileType( GetStdHandle( StdHandle.Stderr ) );
}
}
// P/Invoke:
private enum FileType { Unknown, Disk, Char, Pipe };
private enum StdHandle { Stdin = -10, Stdout = -11, Stderr = -12 };
[DllImport( "kernel32.dll" )]
private static extern FileType GetFileType( IntPtr hdl );
[DllImport( "kernel32.dll" )]
private static extern IntPtr GetStdHandle( StdHandle std );
}
#endregion
#region Error Handling
public static int WriteError( Exception e = null )
{
return WriteError( e == null ? null : e.Message );
}
public static int WriteError( string errorMessage )
{
if ( string.IsNullOrEmpty( errorMessage ) == false )
{
Console.Error.WriteLine( );
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write( "ERROR: " );
Console.ForegroundColor = ConsoleColor.White;
Console.Error.WriteLine( errorMessage );
Console.ResetColor( );
}
Console.Error.WriteLine( );
Console.Error.WriteLine( "ReadLine, Version 0.30 beta" );
Console.Error.WriteLine( "Return the specified line

