代码改变世界

C# 递归

2017-07-14 16:44  伟大的程序员2  阅读(231)  评论(0编辑  收藏  举报

1.ex-1

//递归复制某个路径下的所有文件到指定文件夹内,
public void CopyDirectory(System.IO.DirectoryInfo MySource, System.IO.DirectoryInfo Mytarget)
{
var MyTargetPath = Mytarget.FullName + "\\" + MySource.Name;
if (!System.IO.Directory.Exists(MyTargetPath))
{
System.IO.Directory.CreateDirectory(MyTargetPath);
}


System.IO.FileInfo[] MyFiles = MySource.GetFiles();
foreach(var Myfile in MyFiles)
{
System.IO.File.Copy(Myfile.FullName,MyTargetPath+"\\"+Myfile.Name,true);
}
System.IO.DirectoryInfo[] MySourceDirs = MySource.GetDirectories();
foreach(var MySourceDir in MySourceDirs)
{
var MyTargetDir = new System.IO.DirectoryInfo(MyTargetPath);
CopyDirectory(MySourceDir,MyTargetDir);

}
}

2.ex_2

public int GetDoorStatus(int count)
{
int temp = 1000;
if (count >= 2)
{
temp = GetDoorStatus(count - 1);
if ((temp == 0) || (temp == 1))//执行成功,门开或门关
{
return temp;
}
else if ((temp == -2))//执行异常
{

temp = spf.GetDoorStatus();
switch (temp)
{
case 0:
temp = 0;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行成功且门关");
break;
case 1:
temp = 1;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行成功且门开");
break;
case ErrorCode.ERROR_TIMEOUT:
temp = ErrorCode.ERROR_TIMEOUT;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行超时");
break;
case ErrorCode.ERROR_EXCEPTION:
temp = ErrorCode.ERROR_EXCEPTION;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行异常");
break;
default:
temp = ErrorCode.ERROR_EXCEPTION;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行异常");
break;
}
return temp;
}
}
else if (count == 1)
{
temp = spf.GetDoorStatus();
switch (temp)
{
case 0:
temp = 0;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行成功且门关");
break;
case 1:
temp = 1;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行成功且门开");
break;
case ErrorCode.ERROR_TIMEOUT:
temp = ErrorCode.ERROR_TIMEOUT;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行超时");
break;
case ErrorCode.ERROR_EXCEPTION:
temp = ErrorCode.ERROR_EXCEPTION;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行异常");
break;
default:
temp = ErrorCode.ERROR_EXCEPTION;
LogClassFunctions.SendDataToLog(false, "DLL", "查询门状态", "执行异常");
break;
}
return temp;
}
return temp;

}