using System;
using System . Text;
using System . IO;
using System . Collections;
namespace CMD
{
class Program
{
static void Main ( string [ ] args )
{
Console . Title = @"山寨版:C:\Window\system32\cmd.exe";
Console . WriteLine ( "Backcrosoft Windows [版本 10.11.14.11.2059]" );
Console . WriteLine ( "版权没有 (d) 2010 Backcrosoft Corporation。保留所有权利。" );
//Console . WriteLine ( "2010-11-13[完成]" );
windowsCmd cmd = new windowsCmd ( Directory . GetCurrentDirectory ( ) );
cmd . printPath ( );
string order = Console . ReadLine ( );//读取命令
while ( order != "exit" )
{
cmd . orderAnylise ( order . Trim ( ) );
cmd . printPath ( );
order = Console . ReadLine ( );
}
}
}
class windowsCmd
{
#region 公共对象
private string topFilePath;
private string TopFilePath
{
set
{
if ( value . EndsWith ( "\" ) )
{
topFilePath = value;
}
else
topFilePath = value + "\";
}
get
{
return topFilePath;
}
}//当前的目录路径
private string cmd;//当前主命令
private ArrayList AdressList;//路径列表
private ArrayList ParmaList;//参数列表
private ArrayList ErroList;//错误列表
#endregion
#region Dir对象
bool dirShowAllKids;
long dirAllFileCount;
long dirAllDirectoryCount;
long dirAllFileSize;
long oldCtMinusWh;
#endregion
public windowsCmd ( string Path )
{
TopFilePath = Path;
cmd = "";
AdressList = new ArrayList ( );
ParmaList = new ArrayList ( );
ErroList = new ArrayList ( );
oldCtMinusWh = 0;
}
public void orderAnylise ( string order )//分析命令,确定cmd AdressList ParmaList的值
{
cmd = null;
AdressList . Clear ( );
ParmaList . Clear ( );
ErroList . Clear ( );
if ( order == "" )
return;
for ( int i = 0 ; i < order . Length ; i++ )//获取命令
{
if ( order [ i ] != ' ' && order [ i ] != '/' )
cmd += order [ i ];
else
break;
}
order = order . TrimStart ( cmd . ToCharArray ( ) );//将命令移除,剩下路径和参数
cmd = cmd . ToUpper ( );//将主命令转换为大写形式
if ( order != null )
{
string vStr = null;
int flag = 0;//标识符,0表示无路径或参数 1表示vStr为参数 2表示vStr为路径
for ( int i = 0 ; i < order . Length ; i++ )//获取参数或者路径
{
if ( order [ i ] == '/' || order [ i ] == '|' )
{
switch ( flag )
{
case 0: break;
case 1: ParmaList . Add ( vStr ); vStr = null; break;
case 2: AdressList . Add ( vStr ); vStr = null; break;
}
flag = 1;
}
else if ( order [ i ] == ' ' )
{
switch ( flag )
{
case 0: break;
case 1: ParmaList . Add ( vStr ); vStr = null; break;
case 2: AdressList . Add ( vStr ); vStr = null; break;
}
flag = 2;
vStr += order [ i ];
}
else
vStr += order [ i ];
}
switch ( flag )
{
case 0: break;
case 1: ParmaList . Add ( vStr ); flag = 0; vStr = null; break;
case 2: AdressList . Add ( vStr ); flag = 0; vStr = null; break;
}
}
//移除可能产生的空项
for ( int i = 0 ; i <= AdressList . Count ; i++ )
AdressList . Remove ( null );
for ( int i = 0 ; i <= ParmaList . Count ; i++ )
ParmaList . Remove ( null );
switch ( cmd )//根据命令调用不同的函数
{
case "HELP": help ( ); break;
case "DIR": dir ( ); break;
case "CD": cd ( ); break;
case "CD..": cddotdot ( ); break;
case "CD\": cdBackSlash ( ); break;
case "TYPE": type ( ); break;
case "COPY": copy ( ); break;
case "REN": ren ( ); break;
case "DEL": del ( ); break;
case "MD": md ( ); break;
case "RD": rd ( ); break;
default:
{
ErroList . Add ( ( """ + cmd + " " + order ) . Trim ( ) + """ + "不是内部或外部命令,也不是可运行的程序或批处理文件。" );
ero ( );
break;
}
}
}
protected void help ( )
{
Console . WriteLine ( @"{0}显示一个目录中的文件和子目录。[<文件标识>][<开关参数\w\p\s\a>]" , "DIR" . PadRight ( 8 ) );
Console . WriteLine ( @"{0}显示文本文件的内容。[<盘符>][<路径>]<文件名>] " , "TYPE" . PadRight ( 8 ) );
Console . WriteLine ( @"{0}将至少一个文件复制到另一个位置。" +
"[<盘符>][<路径>]<源文件名> [<盘符>][<路径>]<目标文件名> " , "COPY" . PadRight ( 8 ) );
Console . WriteLine ( @"{0}用于将一个或多个文件更改名称。 [盘符][路径]<原文件名> <新文件名> " , "REN" . PadRight ( 8 ) );
Console . WriteLine ( @"{0}删除至少一个文件。[<盘符>][<路径>]<文件名>] " , "DEL" . PadRight ( 8 ) );
Console . WriteLine ( @"{0}创建一个目录。[盘符][路径]<新目录名>" , "MD" . PadRight ( 8 ) );
Console . WriteLine ( @"{0}显示当前目录的名称或将其更改。[盘符][路径]<文件名>||[..]||[\] " , "CD" . PadRight ( 8 ) );
Console . WriteLine ( @"{0}删除目录。[盘符][路径]<新目录名> " , "RD" . PadRight ( 8 ) );
Console . WriteLine ( @"{0}退出程序" , "exit" . PadRight ( 8 ) );
}
protected void ero ( )
{
if ( ErroList . Count > 0 )
{
foreach ( object x in ErroList )
{
Console . WriteLine ( x . ToString ( ) );
}
}
}
protected void dir ( )//处理dir指令
{
//定义参数
string tFilePath = "";
string wildcard = "*.*"; //通配符
bool showHidden = false;//参数A
bool showInMultClums = false;//参数W
bool showInMultScreen = false;//参数P
bool showAllKids = false;//参数S
bool isWildCard = false;
//确定操作路径
singleAdress ( ref tFilePath );
//确定参数/W/P/A/S
if ( !dirParmar ( ref showInMultClums , ref showAllKids , ref showHidden , ref showInMultScreen ) )
return;//如果存在不符合标准的参数值则退出DIR函数
dirShowAllKids = showAllKids;
wildcard = wildStr ( ref tFilePath , ref isWildCard );//通配符处理
fileOrDirectoryPath ( ref tFilePath , ref wildcard );//判断路径指向文件或文件夹,若是文件则取得父文件夹路径tFilePath及文件名wildcard
if ( Directory . Exists ( tFilePath ) )//判断路径是否存在
{
string [ ] allDirectories = Directory . GetDirectories ( tFilePath , wildcard );
string [ ] allFiles = Directory . GetFiles ( tFilePath , wildcard );
int FilesNum = 0;//文件数量
int DirectoriesNum = 0;//文件夹数量
long FilesSize = 0;//文件总大小(字节)
if ( !isWildCard || allFiles . Length != 0 || allDirectories . Length != 0 )//如果不是通配符模式
{
ifMultScreen ( showInMultScreen );//如果需要分屏,则在必要时暂停输出
Console . WriteLine ( "\n访问的驱动器是" + Directory . GetDirectoryRoot ( tFilePath ) );
ifMultScreen ( showInMultScreen );//如果需要分屏,则在必要时暂停输出
Console . WriteLine ( tFilePath + "的目录" );
DriveInfo di = new DriveInfo ( Directory . GetDirectoryRoot ( tFilePath ) );
long DriverAvailableFreeSpace = di . AvailableFreeSpace;//驱动器剩余空间
foreach ( string x in allDirectories )
{
DirectoryInfo oDir = new DirectoryInfo ( x );
if ( !oDir . Attributes . ToString ( ) . Contains ( "Hidden" ) || showHidden )//决定是否显示隐藏文件
{
ifMultScreen ( showInMultScreen );//如果需要分屏,则在必要时暂停输出
if ( showInMultClums )
dirMultClums_Dir ( x , tFilePath );
else
dirClassic_Directory ( x , tFilePath );
DirectoriesNum++;
}
}
foreach ( string x in allFiles )
{
FileInfo fI = new FileInfo ( x );
if ( !fI . Attributes . ToString ( ) . Contains ( "Hidden" ) || showHidden )
{
ifMultScreen ( showInMultScreen );//如果需要分屏,则在必要时暂停输出
if ( showInMultClums )
dirMultClums_Files ( x , tFilePath );
else
dirClassic_Files ( x , tFilePath );
FilesSize += fI . Length;
FilesNum++;
}
}
ifMultScreen ( showInMultScreen );//如果需要分屏,则在必要时暂停输出
Console . WriteLine ( "\n{0} 个文件 总大小为{1}字节"
, FilesNum . ToString ( ) . PadLeft ( 15 , ' ' )
, FilesSize . ToString ( "n") );
ifMultScreen ( showInMultScreen );//如果需要分屏,则在必要时暂停输出
Console . WriteLine ( "{0} 个目录 可用空间{1}字节"
, DirectoriesNum . ToString ( ) . PadLeft ( 15 , ' ' )
, DriverAvailableFreeSpace . ToString ( "n" ) );
}
else if ( !showAllKids && ( allFiles . Length == 0 || allDirectories . Length == 0 ) )
{
ErroList . Add ( "找不到文件" );
ero ( );
}
if ( showAllKids )//如果需要遍历子目录
{
string parma = "";
foreach ( string y in ParmaList )
parma += "/" + y . ToString ( ) . Trim ( );
string [ ] tAllDirectories = Directory . GetDirectories ( tFilePath );
foreach ( string x in tAllDirectories )
{
DirectoryInfo oDir = new DirectoryInfo ( x );
if ( !oDir . Attributes . ToString ( ) . Contains ( "Hidden" ) || showHidden )//决定是否显示隐藏文件
{
orderAnylise ( "dir " + x + "\" + wildcard + parma );
}
}
dirAllFileCount += FilesNum;
dirAllDirectoryCount += DirectoriesNum;
dirAllFileSize += FilesSize;
}
}
else
{
ErroList . Add ( "找不到文件" );
ero ( );
}
}
protected void cd ( )
{
//定义参数
string tFilePath = "";
//确定操作路径
try
{
for ( int i = 0 ; i < AdressList . Count ; i++ )
{
tFilePath += AdressList [ i ] . ToString ( );
}
tFilePath = tFilePath . Trim ( );
if ( tFilePath == ".." )
{
tFilePath = topFilePath;
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) );
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) );
}
if ( tFilePath == "\" )
{
tFilePath = topFilePath;
tFilePath = tFilePath . Remove ( tFilePath . IndexOf ( "\" ) + 1 );
}
if ( !tFilePath . Contains ( ":" ) || tFilePath . Trim ( ) [ 1 ] != ':' )
tFilePath = delDot ( tFilePath );
if ( Directory . Exists ( tFilePath ) )
TopFilePath = tFilePath . TrimEnd ( '\\' ) + "\";
else
{
ErroList . Add ( "系统找不到指定路径。" );
ero ( );
return;
}
}
catch ( Exception e )
{
Console . WriteLine ( e . Message );
}
}
protected void cddotdot ( )
{
//定义参数
string tFilePath = "";
tFilePath = topFilePath;
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) );
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) );
if ( !tFilePath . Contains ( ":" ) || tFilePath . Trim ( ) [ 1 ] != ':' )
tFilePath = delDot ( tFilePath );
if ( Directory . Exists ( tFilePath ) )
TopFilePath = tFilePath;
else
{
ErroList . Add ( "系统找不到指定路径。" );
ero ( );
return;
}
}
protected void cdBackSlash ( )
{
//定义参数
string tFilePath = "";
tFilePath = topFilePath;
tFilePath = tFilePath . Remove ( tFilePath . IndexOf ( "\" ) + 1 );
if ( !tFilePath . Contains ( ":" ) || tFilePath . Trim ( ) [ 1 ] != ':' )
tFilePath = delDot ( tFilePath );
if ( Directory . Exists ( tFilePath ) )
TopFilePath = tFilePath;
else
{
ErroList . Add ( "系统找不到指定路径。" );
ero ( );
return;
}
}
protected void type ( )
{
string tFilePath = "";
singleAdress ( ref tFilePath );
if ( tFilePath == TopFilePath )
ErroList . Add ( "命令语法不正确" );
else
try
{
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( '\\' ) );
StreamReader sr = new StreamReader ( tFilePath , Encoding . Default );
for ( ; !sr . EndOfStream ; )
Console . WriteLine ( sr . ReadLine ( ) );
}
catch ( FileNotFoundException )
{
ErroList . Add ( "要访问的文件不存在" );
}
catch ( DirectoryNotFoundException )
{
ErroList . Add ( "要访问的文件不存在" );
}
catch ( Exception e )
{
Console . WriteLine ( e . Message );
}
ero ( );
}
protected void copy ( )
{
string filePath1 = "";
string filePath2 = "";
string wild1 = "*.*";//获取第一路径的通配符或文件名
string wild2 = "*.*";//获取第二路径的通配符或文件名
bool isWildCard1 = false;//第一路径是否使用通配符
bool isWildCard2 = false;//第二路径是否使用通配符
long copyCount = 0;//已复制的文件数
bool allowAllOverWrite = false;
if ( !( bool ) getFilePath12 ( ref filePath1 , ref filePath2 , ref wild1 , ref isWildCard1 , ref isWildCard2 ) )
return;//获取第一、第二路径及相关参数
fileOrDirectoryPath ( ref filePath1 , ref wild1 );
string [ ] allFilesPath = Directory . GetFiles ( filePath1 , wild1 );
foreach ( string x in allFilesPath )
{
FileInfo fi = new FileInfo ( x );
try
{
if ( newFileOrOldDirectoryPath ( ref filePath2 , ref wild2 ) )//新名字
if ( Directory . GetFiles ( filePath2 , wild2 ) . LongLength == 0 )//目标文件夹中无重名文件
fi . CopyTo ( filePath2 + wild2 , allowAllOverWrite );
else
throw new NotImplementedException ( );
else
if ( Directory . GetFiles ( filePath2 , x . Replace ( filePath1 , "" ) ) . LongLength == 0 )//源文件名与目标文件名不同
fi . CopyTo ( filePath2 + x . Replace ( filePath1 , "" ) , allowAllOverWrite );//保持原名
else
throw new NotImplementedException ( );
copyCount++;
}
catch ( NotImplementedException )//对重名的处理
{
try
{
if ( allowAllOverWrite )
{
if ( newFileOrOldDirectoryPath ( ref filePath2 , ref wild2 ) )//新名字
fi . CopyTo ( filePath2 + wild2 , true );
else
fi . CopyTo ( filePath2 + x . Replace ( filePath1 , "" ) , true );//保持原名
copyCount++;
}
else
{
重名://重名标签
Console . WriteLine ( "目标:{0}" , x );
Console . WriteLine ( "文件已存在,是否覆盖?(Yes\\All\\No)" );
switch ( Console . ReadLine ( ) . ToUpper ( ) )
{
case "YES":
case "Y":
{
if ( newFileOrOldDirectoryPath ( ref filePath2 , ref wild2 ) )//新名字
fi . CopyTo ( filePath2 + wild2 , true );
else
fi . CopyTo ( filePath2 + x . Replace ( filePath1 , "" ) , true );//保持原名
copyCount++;
break;
}
case "NO":
case "N": allowAllOverWrite = false; break;
case "ALL":
case "A":
{
allowAllOverWrite = true;
if ( newFileOrOldDirectoryPath ( ref filePath2 , ref wild2 ) )//新名字
fi . CopyTo ( filePath2 + wild2 , true );
else
fi . CopyTo ( filePath2 + x . Replace ( filePath1 , "" ) , true );//保持原名
copyCount++;
break;
}
default: goto 重名;
}
}
}
catch
{
Console . WriteLine ( "目标:{0}" , x );
ErroList . Add ( "目标文件正在使用中。" );
ero ( );
}
}
catch ( IOException )
{
Console . WriteLine ( "目标:{0}" , x );
ErroList . Add ( "目标文件正在使用中。" );
ero ( );
}
}
Console . WriteLine ( "已复制{0}个文件" , copyCount . ToString ( ) . PadLeft ( 10 ) );
}
protected void ren ( )
{
string tFilePath = "";
string filePath1 = "";
string newName = "";
string wild1 = "*.*";//获取第一路径的通配符或文件名
bool isWildCard1 = false;//第一路径是否使用通配符
foreach ( string x in AdressList )
{
tFilePath += x;
}
tFilePath = tFilePath . Trim ( );
string [ ] str = tFilePath . Split ( ' ' );
tFilePath = "";
for ( int i = 0 ; i < str . Length ; i++ )
{
tFilePath += " " + str [ i ];
if ( !isWildCard1 )//仅有最后一段允许通配符,若遇到通配符则认为第一路径结束
{
wild1 = wildStr ( ref tFilePath , ref isWildCard1 );//通配符处理
}
if ( Directory . Exists ( tFilePath ) || File . Exists ( tFilePath )
|| File . Exists ( TopFilePath + tFilePath . Trim ( ) ) )
{
filePath1 = tFilePath;
}
}
if ( filePath1 == "" )
{
ErroList . Add ( "路径不存在" );
ero ( );
return;
}
newName = tFilePath . Remove ( 0 , filePath1 . Length );
newName = newName . Trim ( );
filePath1 = filePath1 . Trim ( );
if ( !filePath1 . Contains ( ":" ) || filePath1 . Trim ( ) [ 1 ] != ':' )
{
filePath1 = delDot ( filePath1 );
}
if ( !filePath1 . EndsWith ( "\" ) )
filePath1 += "\";
fileOrDirectoryPath ( ref filePath1 , ref wild1 );
string [ ] filesPath = Directory . GetFiles ( filePath1 , wild1 );
if ( !wild1 . Contains ( "*" ) && !wild1 . Contains ( "?" ) )//单文件重命名
{
foreach ( string x in filesPath )
{
try
{
FileInfo fi = new FileInfo ( x );
fi . MoveTo ( filePath1 + newName );
}
catch ( IOException )
{
ErroList . Add ( "目标文件访问失败" );
ero ( );
return;
}
catch ( ArgumentException )
{
ErroList . Add ( "文件名中含有非法字符" );
ero ( );
return;
}
}
}
else //批量重命名
{
string wildSequence1 = "";
string wildSequence2 = "";
foreach ( char x in wild1 )
{
if ( x == '*' || x == '?' )
{
wildSequence1 += x;
}
}
foreach ( char x in newName )
{
if ( x == '*' || x == '?' )
{
wildSequence2 += x;
}
}
if ( wildSequence1 != wildSequence2 )
{
ErroList . Add ( "批量重命名语法出错" );
ero ( );
return;
}
//*************依据通配符分割
string [ ] wildSplit1 = wild1 . Split ( "*?" . ToCharArray ( ) , StringSplitOptions . None );
string [ ] wildSplit2 = newName . Split ( "*?" . ToCharArray ( ) , StringSplitOptions . None );
foreach ( string xx in filesPath )
{
//根据通配符获得新的tNewName
string tNewName = "";
tNewName += xx;
tNewName = tNewName . Replace ( filePath1 , "" );//此时tNewName=原来的文件名
for ( int i = 0 ; i < wildSplit1 . Length ; i++ )
{
int stra = tNewName . IndexOf ( wildSplit1 [ i ] );
tNewName = tNewName . Remove ( stra , wildSplit1 [ i ] . Length );
tNewName = tNewName . Insert ( stra , wildSplit2 [ i ] );
}
try
{
FileInfo fi = new FileInfo ( xx );
//*******根据tNewName给源文件重命名
fi . MoveTo ( filePath1 + tNewName );
}
catch ( IOException )
{
ErroList . Add ( "目标文件访问失败" );
ero ( );
return;
}
catch ( ArgumentException )
{
ErroList . Add ( "文件名中含有非法字符" );
ero ( );
return;
}
}
}
}
protected void del ( )
{
//定义参数
string tFilePath = "";
string wildcard = "*.*"; //通配符
bool isWildCard = false;
//确定操作路径
singleAdress ( ref tFilePath );
wildcard = wildStr ( ref tFilePath , ref isWildCard );//通配符处理
if ( fileOrDirectoryPath ( ref tFilePath , ref wildcard ) )//判断路径指向文件或文件夹,若是文件则取得父文件夹路径tFilePath及文件名wildcard
{//若指向文件
if ( Directory . Exists ( tFilePath ) )//判断路径是否存在
{
string [ ] allFiles = Directory . GetFiles ( tFilePath , wildcard );
foreach ( string x in allFiles )
{
FileInfo fI = new FileInfo ( x );
fI . Delete ( );
}
}
}
else
{
if ( Directory . Exists ( tFilePath ) )//判断路径是否存在
{
string [ ] allFiles = Directory . GetFiles ( tFilePath );
询问:
Console . Write ( tFilePath + "\\*, 是否确认(Y/N)? " );
switch ( Console . ReadLine ( ) . ToLower ( ) )
{
case "y":
{
foreach ( string x in allFiles )
{
FileInfo fI = new FileInfo ( x );
fI . Delete ( );
}
break;
}
case "n": return;
default: goto 询问;
}
}
else
{
ErroList . Add ( "找不到文件" );
ero ( );
return;
}
}
}
protected void md ( )
{
//定义参数
string tFilePath = "";
//确定操作路径
singleAdress ( ref tFilePath );
try
{
if ( !Directory . Exists ( tFilePath ) )
Directory . CreateDirectory ( tFilePath );
else
ErroList . Add ( "该目录已存在(不区分大小写)" );
ero ( );
}
catch ( ArgumentException )
{
ErroList . Add ( "新目录名中含有非法字符" );
ero ( );
return;
}
catch ( Exception e )
{
Console . WriteLine ( e . Message );
}
}
protected void rd ( )
{
//定义参数
string tFilePath = "";
Stack st = new Stack ( );//存放目录地址的堆栈
//确定操作路径
singleAdress ( ref tFilePath );
try
{
if ( Directory . Exists ( tFilePath ) )
{
//由于Delete()方法仅能用于空目录
//所以利用堆栈模拟递归,清空目录及子目录下所有的文件
//并将所有目录的路径压入堆栈
st . Push ( tFilePath );
while ( st . Count != 0 )
{
string [] tDirectorys = Directory . GetDirectories ( st . Peek ( ) . ToString ( ) );
string [] tFiles = Directory . GetFiles ( st . Peek ( ) . ToString ( ) );
if ( tDirectorys . Length == 0 && tFiles . Length == 0 )//当前目录已为空目录
Directory . Delete ( st . Pop ( ) . ToString ( ) );//删除并在堆栈弹出当前目录
foreach ( var x in tDirectorys )
{
st . Push ( x );//把目录地址压入堆栈
}
foreach ( var x in tFiles )
{
File . Delete ( x );//清空目录下所有文件(文件夹被保留)
}
}
}
}
catch ( Exception e )
{
Console . WriteLine ( e . Message );
}
}
#region dir系列函数
private void dirClassic_Directory ( string x , string tFilePath )
{
Console . WriteLine ( "{0} {1}
{2}"
, Directory . GetLastWriteTime ( x ) . ToString ( "yyyy-MM-dd" )
, Directory . GetLastWriteTime ( x ) . ToString ( "HH:mm" ) . PadLeft ( 5 , ' ' )
, x . Replace ( tFilePath , "" ) );
}
private void dirClassic_Files ( string x , string tFilePath )
{
Console . WriteLine ( "{0} {1} {2}"
, Directory . GetLastWriteTime ( x ) . ToString ( "yyyy-MM-dd" )
, Directory . GetLastWriteTime ( x ) . ToString ( "HH:mm" ) . PadLeft ( 5 , ' ' )
, x . Replace ( tFilePath , "" ) );
}
private void dirMultClums_Dir ( string x , string tFilePath )
{
if ( Console . CursorLeft + x . Replace ( tFilePath , "" ) . Length >= Console . WindowWidth - 4 )
Console . Write ( "\n" );
Console . Write ( "[{0}] " , x . Replace ( tFilePath , "" ) );
}
private void dirMultClums_Files ( string x , string tFilePath )
{
if ( Console . CursorLeft + x . Replace ( tFilePath , "" ) . Length >= Console . WindowWidth - 4 )
Console . Write ( "\n" );
Console . Write ( "{0} " , x . Replace ( tFilePath , "" ) );
}
private bool dirParmar ( ref bool showInMultClums , ref bool showAllKids , ref bool showHidden , ref bool showInMultScreen )//参数分析
{
foreach ( object x in ParmaList )
{
switch ( x . ToString ( ) . Trim ( ) . ToUpper ( ) )
{
case "W": showInMultClums = true; break;
case "S": showAllKids = true; break;
case "A": showHidden = true; break;
case "P": showInMultScreen = true; break;
default: ErroList . Add ( "无法识别的参数:" + x ); return false;
}
}
return true;
}
#endregion
#region copy系列函数
protected void copyOrMoveDirectory ( string DirectoryPath , string DirAddress , bool DirFirst )//复制\移动文件夹 DirFirst=true表复制,false表移动
{
if ( DirectoryPath . EndsWith ( "\" ) )
DirectoryPath = DirectoryPath . Remove ( DirectoryPath . LastIndexOf ( "\" ) );
string s = DirectoryPath . Substring ( DirectoryPath . LastIndexOf ( "\" ) + 1 );//获取文件夹名
DirectoryInfo DirectoryArray = new DirectoryInfo ( DirectoryPath );
FileInfo [ ] Files = DirectoryArray . GetFiles ( );//获取该文件夹下的文件列表
DirectoryInfo [ ] Directorys = DirectoryArray . GetDirectories ( );//获取该文件夹下的文件夹列表
if ( DirFirst )//复制时
{
if ( Directory . Exists ( DirAddress ) )
{
//Directory.Delete(DirAddress, true);//若文件夹存在,不管目录是否为空,删除
//Directory.CreateDirectory(DirAddress);//删除后,重新创建文件夹
}
else
{
Directory . CreateDirectory ( DirAddress );//文件夹不存在,创建
}
foreach ( FileInfo inf in Files )//逐个复制文件
{
if ( !File . Exists ( DirAddress + "\" + inf . Name ) )
File . Copy ( DirectoryPath + "\" + inf . Name , DirAddress + "\" + inf . Name );
}
foreach ( DirectoryInfo Dir in Directorys )//逐个获取文件夹名称,并递归调用方法本身
{
copyOrMoveDirectory ( DirectoryPath + "\" + Dir . Name , DirAddress , DirFirst );
}
}
else
{
if ( Directory . Exists ( DirAddress + "\" + s ) )
{
//Directory . Delete ( DirAddress + "\" + s , true );//若文件夹存在,不管目录是否为空,删除
//Directory . CreateDirectory ( DirAddress + "\" + s );//删除后,重新创建文件夹
}
else
{
Directory . CreateDirectory ( DirAddress + "\" + s );//文件夹不存在,创建
}
foreach ( FileInfo inf in Files )//逐个复制文件
{
if ( !File . Exists ( DirAddress + "\" + inf . Name ) )
File . Copy ( DirectoryPath + "\" + inf . Name , DirAddress + "\" + s + "\" + inf . Name );
}
foreach ( DirectoryInfo Dir in Directorys )//逐个获取文件夹名称,并递归调用方法本身
{
copyOrMoveDirectory ( DirectoryPath + "\" + Dir . Name , DirAddress + "\" + s , DirFirst );
}
}
}
protected object getFilePath12 ( ref string filePath1 , ref string filePath2 , ref string wild1 , ref bool isWildCard1 , ref bool isWildCard2 )
{
string tFilePath = "";
foreach ( string x in AdressList )
{
tFilePath += x;
}
tFilePath = tFilePath . Trim ( );
string [ ] str = tFilePath . Split ( ' ' );
tFilePath = "";
for ( int i = 0 ; i < str . Length ; i++ )
{
tFilePath += " " + str [ i ];
if ( !isWildCard1 )//仅有最后一段允许通配符,若遇到通配符则认为第一路径结束
{
wild1 = wildStr ( ref tFilePath , ref isWildCard1 );//通配符处理
}
if ( Directory . Exists ( tFilePath ) || File . Exists ( tFilePath )
|| File . Exists ( TopFilePath + tFilePath . Trim ( ) ) )
{
filePath1 = tFilePath;
}
}
if ( filePath1 == "" )
{
ErroList . Add ( "路径不存在" );
ero ( );
return false;
}
filePath2 = tFilePath . Remove ( 0 , filePath1 . Length );
filePath2 = filePath2 . Trim ( );
filePath1 = filePath1 . Trim ( );
if ( !filePath1 . Contains ( ":" ) || filePath1 . Trim ( ) [ 1 ] != ':' )
{
filePath1 = delDot ( filePath1 );
}
if ( !filePath2 . Contains ( ":" ) || filePath2 . Trim ( ) [ 1 ] != ':' )
{
filePath2 = delDot ( filePath2 );
}
wildStr ( ref filePath2 , ref isWildCard2 );//通配符处理
if ( isWildCard2 )
{
ErroList . Add ( "命令语法不正确" );
ero ( );
return false;//第二路径不允许使用通配符
}
if ( !filePath1 . EndsWith ( "\" ) )
filePath1 += "\";
if ( !filePath2 . EndsWith ( "\" ) )
filePath2 += "\";
//**********以上已成功取得filePath1、2
return true;
}
#endregion
#region 地址处理函数
protected string delDot ( string path )//输入含有".."的地址,返回符合一般规范的地址
{
if ( path . Contains ( ".." ) )
{
if ( !path . Replace ( ".." , "" ) . Contains ( "." ) )
{
string tPath = TopFilePath;
if ( tPath . EndsWith ( "\" ) )
{
tPath = tPath . Remove ( tPath . Length - 1 );
}
while ( path . StartsWith ( "..\" ) && tPath . Contains ( "\" ) )
{
tPath = tPath . Remove ( tPath . LastIndexOf ( '\\' ) );
path = path . TrimStart ( "..\" . ToCharArray ( ) );
}
if ( !tPath . EndsWith ( "\" ) )
{
tPath += "\";
}
tPath += path;
return tPath;
}
else
return "false";
}
else
return topFilePath + path;
}
protected void testAdressPar ( )
{
ParmaList . Remove ( null );
AdressList . Remove ( null );
foreach ( object x in ParmaList )
{
Console . WriteLine ( x . ToString ( ) + "" );
}
Console . WriteLine ( "adressssssssssssssssssssssssssssssssssssssssssssssssss" );
foreach ( object x in AdressList )
{
Console . WriteLine ( x . ToString ( ) + "" );
}
}
protected void singleAdress ( ref string tFilePath )//单地址情况处理
{
try
{
for ( int i = 0 ; i < AdressList . Count ; i++ )
{
tFilePath += AdressList [ i ] . ToString ( );
}
tFilePath = tFilePath . Trim ( );
if ( !tFilePath . Contains ( ":" ) || tFilePath . Trim ( ) [ 1 ] != ':' )
{
tFilePath = delDot ( tFilePath );
}
}
catch ( Exception e )
{
Console . WriteLine ( e . Message );
}
if ( tFilePath == "" )
tFilePath = topFilePath;
if ( !tFilePath . EndsWith ( "\" ) )
tFilePath += "\";
}
protected bool fileOrDirectoryPath ( ref string tFilePath , ref string wildcard )//判断路径指向文件夹还是文件
{
if ( File . Exists ( tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) ) ) )
{
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) );
wildcard = tFilePath . Substring ( tFilePath . LastIndexOf ( "\" ) + 1 );
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) + 1 );
return true;//指向文件
}
return false;//指向文件夹
}
protected bool newFileOrOldDirectoryPath ( ref string tFilePath , ref string wildcard )//判断路径指向文件夹还是新文件
{
if ( !Directory . Exists ( tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) ) ) )
{
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) );
wildcard = tFilePath . Substring ( tFilePath . LastIndexOf ( "\" ) + 1 );
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( "\" ) + 1 );
return true;//指向新文件
}
return false;//指向文件夹
}
#endregion
#region 公共函数
protected void ifMultScreen ( bool showInMultScreen )
{
if ( ( Console . CursorTop / Console . WindowHeight > oldCtMinusWh ) && showInMultScreen )//决定是否分屏显示
{
pressAnyKey ( );
oldCtMinusWh = Console . CursorTop / Console . WindowHeight;
}//读取任意字符继续
}
protected string wildStr ( ref string tFilePath , ref bool isWildCard )
{
string wildcard = "*.*";
if ( tFilePath . Contains ( "*" ) || tFilePath . Contains ( "?" ) )//针对通配符
{
if ( tFilePath . EndsWith ( "\" ) )
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( '\\' ) );
if ( tFilePath . IndexOf ( '*' ) >= tFilePath . LastIndexOf ( '\\' )
|| tFilePath . IndexOf ( '?' ) >= tFilePath . LastIndexOf ( '\\' ) )//只有最后的文件或目录支持通配符
{
wildcard = tFilePath . Substring ( tFilePath . LastIndexOf ( '\\' ) + 1 );
tFilePath = tFilePath . Remove ( tFilePath . LastIndexOf ( '\\' ) + 1 );
isWildCard = true;
}
else
{
ErroList . Add ( "通配符使用错误" );
}
}
return wildcard;
}
protected void pressAnyKey ( )
{
Console . Write ( "按任意键继续" );
ConsoleKeyInfo xxx = Console . ReadKey ( true );
Console . WriteLine ( "" );
}
public void printPath ( )//显示当前默认路径
{
if ( dirShowAllKids )
{
Console . WriteLine ( "\n{0}个文件 总大小为{1}字节"
, ( "总计" + dirAllFileCount . ToString ( ) ) . PadLeft ( 15 , ' ' )
, dirAllFileSize . ToString ( ) );
Console . WriteLine ( "{0}个目录"
, ( "总计" + dirAllDirectoryCount . ToString ( ) ) . PadLeft ( 15 , ' ' ) );
dirShowAllKids = false;
dirAllFileSize = 0;
dirAllFileCount = 0;
dirAllDirectoryCount = 0;
}
Console . Write ( "\n" + TopFilePath + ">" );
}
#endregion
}
}