扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace IISControlService
{
public class Utility
{
public static void LogToFile(string msg, LogState state)
{
try
{
string _logfile = System.Configuration.ConfigurationManager.AppSettings["logfile"];
if (!Directory.Exists(_logfile))
{
Directory.CreateDirectory(_logfile);
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
switch (state)
{
case LogState.Success:
sb.Append("[成功]\t");
break;
case LogState.Failed:
sb.Append("[失败]\t");
break;
case LogState.Message:
sb.Append("[消息]\t");
break;
default:
break;
}
sb.Append(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
sb.Append("\t");
sb.Append(msg);
_logfile = Path.Combine(_logfile, DateTime.Now.ToString("yyyyMMdd") + ".log");
StreamWriter sw = new StreamWriter(_logfile, true);
sw.WriteLine(sb.ToString());
sw.Close();
}
catch
{
}
}
public static bool CopyFiles(string sourcePath, string targetPath)
{
sourcePath = sourcePath.TrimEnd('\\') + "\\";
targetPath = targetPath.TrimEnd('\\') + "\\";
DirectoryInfo dir = new DirectoryInfo(sourcePath);
if (dir.Exists)
{
DirectoryInfos dirs = new DirectoryInfos();
dirs.Add(dir);
//开始复制文件
while (dirs.Count > 0)//foreach(DirectoryInfo subdir in dirs)
{
DirectoryInfo subdir = dirs[0];
try
{
Directory.CreateDirectory(subdir.FullName.Replace(sourcePath, targetPath));
}
catch (Exception err)
{
//创建目录失败,写入日志,返回false;
LogToFile("创建目录[" + subdir.FullName.Replace(sourcePath, targetPath) + "]失败,错误原因:" + err.Message, LogState.Failed);
return false;
}
foreach (FileInfo file in subdir.GetFiles())
{
try
{
file.CopyTo(file.FullName.Replace(sourcePath, targetPath));
}
catch (Exception err)
{
//复制文件失败,写入日志,返回false;
LogToFile("复制文件[" + file.FullName.Replace(sourcePath, targetPath) + "]失败,错误原因:" + err.Message, LogState.Failed);
return false;
}
}
dirs.AddCollection(subdir.GetDirectories());
dirs.RemoveAt(0);
}
return true;
}
else
{
LogToFile("创建目录[" + targetPath + "]失败,错误原因:指定的源文件不存在。", LogState.Failed);
return false;
}
}
}
public class DirectoryInfos : System.Collections.CollectionBase
{
public int Add(DirectoryInfo di)
{
return this.List.Add(di);
}
public DirectoryInfo this[int index]
{
get { return (DirectoryInfo)List[index]; }
}
public void AddCollection(DirectoryInfo[] dirs)
{
foreach (DirectoryInfo dir in dirs)
{
List.Add(dir);
}
}
}
public enum LogState
{
Success,
Failed,
Message
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1683863
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者