C# 递归

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;

}

转载于:https://www.cnblogs.com/Cpart/p/7171274.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值