/// <summary>
/// ***************************
/// ****操作文本文件或日志文件封装类 *****
/// **** V1.0 12-07-27 *****
/// **** 狼鹰组织 *****
/// **** hyxwill *****
/// ***************************
/// </summary>
internal sealed class ReadWriteLog:BaseRWFile
{
internal void Add(string path, string name, string content)
{
if (base.QueryFolderIt(path, name))
{
AdditionalDocuments(path, name, content);
}
else
{
AddFile(path, name, content);
}
}
private void AddFile(string path, string name, string content)
{
base.WriteFile(string.Concat(path,"\\",name), content);
}
/// <summary>
/// 追加文件
/// </summary>
private void AdditionalDocuments(string path, string name, string content)
{
base.AdditionalDocuments(string.Concat(path, "\\", name), content);
}
/// <summary>
/// 追加文件
/// </summary>
private new void AdditionalDocuments(string Server, string[] srclist)
{
base.AdditionalDocuments(Server, srclist);
}
/// <summary>
/// 删除
/// </summary>
internal new void DeleteFile(string delFile)
{
base.DeleteFile(delFile);
}
}
internal class BaseRWFile
{
static string TextError = string.Empty;
/// <summary>
/// 取得指定文件夹内的文件
/// </summary>
/// <param name="LocSrc">类C://ABC/DEF//*.*</param>
/// <returns></returns>
internal string[] getFileAllName(string LocSrc)
{
try
{
return Directory.GetFiles(@"" + LocSrc);
}
catch (Exception)
{
return null;
}
}
#region 文件类
/// <summary>
/// 读,单
/// </summary>
internal string ReadFile(string Server)
{
try
{
string rtxt = "";
StreamReader sr = File.OpenText(Server);
string txt = "";
while ((txt = sr.ReadLine()) != null)
{
rtxt = string.Concat(rtxt, txt);
}
sr.Close();
sr.Dispose();
return rtxt;
}
catch (Exception)
{
return TextError;
}
}
/// <summary>
/// 读,集合
/// </summary>
/// <param name="Server"></param>
/// <returns></returns>
internal List<string> ReadFileArray(string Server)
{
try
{
StreamReader sr = File.OpenText(Server);
string txt = "";
List<string> ls = new List<string>();
while ((txt = sr.ReadLine()) != null)
{
if (txt != "")
ls.Add(txt);
}
sr.Close();
sr.Dispose();
return ls;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 写
/// </summary>
internal bool WriteFile(string Server, string Src)
{
try
{
StreamWriter sw = new StreamWriter(Server);
sw.WriteLine(Src);
sw.Close();
sw.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 追加文件
/// </summary>
internal bool AdditionalDocuments(string Server, string[] srclist)
{
try
{
StreamWriter sw = File.AppendText(Server);
foreach (string s in srclist)
sw.WriteLine(s);
sw.Flush();
sw.Close();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 追加文件
/// </summary>
internal bool AdditionalDocuments(string Server, string srclist)
{
try
{
StreamWriter sw = File.AppendText(Server);
sw.WriteLine(srclist);
sw.Flush();
sw.Close();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 拷贝文件
/// </summary>
internal void CopyFile(string OrignFile, string NewFile)
{
try
{
File.Copy(OrignFile, NewFile, true);
}
catch (Exception)
{
TextError = "error";
}
}
/// <summary>
/// 删除
/// </summary>
internal void DeleteFile(string delFile)
{
try
{
File.Delete(delFile);
}
catch (Exception)
{
TextError = "error";
}
}
/// <summary>
/// 移动
/// </summary>
internal void MoveFile(string OrignFile, string NewFile)
{
try
{
File.Move(OrignFile, NewFile);
}
catch (Exception)
{
}
}
#endregion
#region 文件夹类
/// <summary>
/// 处理创建文件夹,创建单的
/// </summary>
/// <param name="dname"></param>
/// <param name="dfile"></param>
/// <param name="iffor">是否要循环一条龙创建</param>
internal bool CreateFiler(string LocSrc, string FilerName,bool iffor)
{
if (iffor)
{
try
{
DirectoryInfo d = Directory.CreateDirectory(string.Concat(LocSrc,"\\", FilerName));
return true;
}
catch (Exception)
{
string thispath = LocSrc.Substring(0, LocSrc.LastIndexOf('\\'));
string folername = LocSrc.Substring(LocSrc.LastIndexOf('\\') + 1);
folername.Replace("\\", "");
while (CreateFiler(thispath, folername, true))
{
break;
}
//CreateFiler(LocSrc.Substring(0, LocSrc.LastIndexOf('\\')) + "\\", LocSrc.Substring(LocSrc.LastIndexOf('\\') + 1),true);
return true;
}
}
else
{
try
{
DirectoryInfo d = Directory.CreateDirectory(string.Concat(LocSrc, FilerName));
return true;
}
catch (Exception)
{
return false;
}
}
}
/// <summary>
/// 处理创建文件夹,创建单的
/// </summary>
/// <param name="dname"></param>
/// <param name="dfile"></param>
internal bool CreateFiler(string LocSrc, string FilerName)
{
try
{
DirectoryInfo d = Directory.CreateDirectory(string.Concat(LocSrc, FilerName));
return true;
}
catch (Exception)
{
CreateFiler(LocSrc, FilerName, true);
return false;
}
}
/// <summary>
/// 处理创建文件夹,处理几重的文件夹(直线创建)
/// </summary>
/// <param name="dname">c:\\式</param>
/// <param name="dfile"></param>
internal bool CreateFiler(string LocSrc, string[] ParmFilerName)
{
string src = LocSrc;
try
{
foreach (string s in ParmFilerName)
{
src = string.Concat(src, "\\", s);
Directory.CreateDirectory(src);
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 处理创建文件夹,处理几重的文件夹(分式创建)
/// </summary>
/// <param name="dname"></param>
/// <param name="dfile"></param>
internal bool CreateFiler(string LocSrc, string BaseFilerName, string[] ParmFilerName)
{
try
{
string src = string.Concat(LocSrc, "\\", BaseFilerName);
Directory.CreateDirectory(src);
Directory.SetCurrentDirectory(src);
foreach (string s in ParmFilerName)
{
Directory.CreateDirectory(s);
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 处理创建文件夹,处理几重的文件夹(多重直分式创建)
/// </summary>
/// <param name="dname"></param>
/// <param name="dfile"></param>
internal bool CreateFiler(string LocSrc, List<string> BaseFilerName, List<string[]> ParmFilerName)
{
try
{
for (int i = 0; i < BaseFilerName.Count; i++)
{
CreateFiler(LocSrc, BaseFilerName[i], ParmFilerName[i]);
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 递归删除文件夹及文件
/// </summary>
/// <param name="dir"></param>
internal void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir); //删除已空文件夹
TextError = dir + " 文件夹删除成功";
}
else
TextError = dir + " 该文件夹不存在"; //如果文件夹不存在则提示
}
/// <summary>
/// // ======================================================
/// 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面
/// 如果目标文件夹为只读属性就会报错。
/// ======================================================
/// </summary>
/// <param name="srcPath"></param>
/// <param name="aimPath"></param>
internal void CopyDir(string srcPath, string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
// 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
// string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (Directory.Exists(file))
CopyDir(file, aimPath + Path.GetFileName(file));
// 否则直接Copy文件
else
File.Copy(file, aimPath + Path.GetFileName(file), true);
}
}
catch (Exception e)
{
TextError = e.ToString();
}
}
/// <summary>
/// // ======================================================
/// 实现一个静态方法将指定文件夹下面的所有内容Detele
/// 测试的时候要小心操作,删除之后无法恢复。
/// ======================================================
/// </summary>
/// <param name="aimPath"></param>
internal void DeleteDir(string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
// 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
// string[] fileList = Directory.GetFiles(aimPath);
string[] fileList = Directory.GetFileSystemEntries(aimPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件
if (Directory.Exists(file))
{
DeleteDir(aimPath + Path.GetFileName(file));
}
// 否则直接Delete文件
else
{
File.Delete(aimPath + Path.GetFileName(file));
}
}
//删除文件夹
System.IO.Directory.Delete(aimPath, true);
}
catch (Exception e)
{
TextError = e.ToString();
}
}
/// <summary>
/// 拷贝文件夹(包括子文件夹)到指定文件夹下,源文件夹和目标文件夹均需绝对路径. 格式: CopyFolder(源文件夹,目标文件夹);
/// </summary>
/// <param name="strFromPath"></param>
/// <param name="strToPath"></param>
internal void CopyFolder(string strFromPath, string strToPath)
{
//如果源文件夹不存在,则创建
if (!Directory.Exists(strFromPath))
{
Directory.CreateDirectory(strFromPath);
}
//取得要拷贝的文件夹名
string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("\\") + 1, strFromPath.Length - strFromPath.LastIndexOf("\\") - 1);
//如果目标文件夹中没有源文件夹则在目标文件夹中创建源文件夹
if (!Directory.Exists(strToPath + "\\" + strFolderName))
{
Directory.CreateDirectory(strToPath + "\\" + strFolderName);
}
//创建数组保存源文件夹下的文件名
string[] strFiles = Directory.GetFiles(strFromPath);
//循环拷贝文件
for (int i = 0; i < strFiles.Length; i++)
{
//取得拷贝的文件名,只取文件名,地址截掉。
string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("\\") + 1, strFiles[i].Length - strFiles[i].LastIndexOf("\\") - 1);
//开始拷贝文件,true表示覆盖同名文件
File.Copy(strFiles[i], strToPath + "\\" + strFolderName + "\\" + strFileName, true);
}
//创建DirectoryInfo实例
DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
//取得源文件夹下的所有子文件夹名称
DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
for (int j = 0; j < ZiPath.Length; j++)
{
//获取所有子文件夹名
string strZiPath = strFromPath + "\\" + ZiPath[j].ToString();
//把得到的子文件夹当成新的源文件夹,从头开始新一轮的拷贝
CopyFolder(strZiPath, strToPath + "\\" + strFolderName);
}
}
/// <summary>
/// 查找是否有这个文件夹
/// </summary>
/// <param name="strPath">路径</param>
/// <param name="folderName">文件夹名</param>
/// <returns></returns>
internal bool QueryFolderIt(string strPath, string folderName)
{
try
{
DirectoryInfo dires = new DirectoryInfo(strPath);
FileInfo[] files = dires.GetFiles();
foreach (FileInfo f in files)
{
if (f.Name.ToLower() == folderName.ToLower())
return true;//yield return 是要返回这个 IEnumerable<T>
}
return false;
}
catch (Exception)
{
//出错一般是没有文件夹
CreateFiler(strPath.Substring(0, strPath.LastIndexOf('\\'))+"\\", strPath.Substring(strPath.LastIndexOf('\\') + 1));
return false;
}
}
#endregion
}
/// ***************************
/// ****操作文本文件或日志文件封装类 *****
/// **** V1.0 12-07-27 *****
/// **** 狼鹰组织 *****
/// **** hyxwill *****
/// ***************************
/// </summary>
internal sealed class ReadWriteLog:BaseRWFile
{
internal void Add(string path, string name, string content)
{
if (base.QueryFolderIt(path, name))
{
AdditionalDocuments(path, name, content);
}
else
{
AddFile(path, name, content);
}
}
private void AddFile(string path, string name, string content)
{
base.WriteFile(string.Concat(path,"\\",name), content);
}
/// <summary>
/// 追加文件
/// </summary>
private void AdditionalDocuments(string path, string name, string content)
{
base.AdditionalDocuments(string.Concat(path, "\\", name), content);
}
/// <summary>
/// 追加文件
/// </summary>
private new void AdditionalDocuments(string Server, string[] srclist)
{
base.AdditionalDocuments(Server, srclist);
}
/// <summary>
/// 删除
/// </summary>
internal new void DeleteFile(string delFile)
{
base.DeleteFile(delFile);
}
}
internal class BaseRWFile
{
static string TextError = string.Empty;
/// <summary>
/// 取得指定文件夹内的文件
/// </summary>
/// <param name="LocSrc">类C://ABC/DEF//*.*</param>
/// <returns></returns>
internal string[] getFileAllName(string LocSrc)
{
try
{
return Directory.GetFiles(@"" + LocSrc);
}
catch (Exception)
{
return null;
}
}
#region 文件类
/// <summary>
/// 读,单
/// </summary>
internal string ReadFile(string Server)
{
try
{
string rtxt = "";
StreamReader sr = File.OpenText(Server);
string txt = "";
while ((txt = sr.ReadLine()) != null)
{
rtxt = string.Concat(rtxt, txt);
}
sr.Close();
sr.Dispose();
return rtxt;
}
catch (Exception)
{
return TextError;
}
}
/// <summary>
/// 读,集合
/// </summary>
/// <param name="Server"></param>
/// <returns></returns>
internal List<string> ReadFileArray(string Server)
{
try
{
StreamReader sr = File.OpenText(Server);
string txt = "";
List<string> ls = new List<string>();
while ((txt = sr.ReadLine()) != null)
{
if (txt != "")
ls.Add(txt);
}
sr.Close();
sr.Dispose();
return ls;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 写
/// </summary>
internal bool WriteFile(string Server, string Src)
{
try
{
StreamWriter sw = new StreamWriter(Server);
sw.WriteLine(Src);
sw.Close();
sw.Dispose();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 追加文件
/// </summary>
internal bool AdditionalDocuments(string Server, string[] srclist)
{
try
{
StreamWriter sw = File.AppendText(Server);
foreach (string s in srclist)
sw.WriteLine(s);
sw.Flush();
sw.Close();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 追加文件
/// </summary>
internal bool AdditionalDocuments(string Server, string srclist)
{
try
{
StreamWriter sw = File.AppendText(Server);
sw.WriteLine(srclist);
sw.Flush();
sw.Close();
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 拷贝文件
/// </summary>
internal void CopyFile(string OrignFile, string NewFile)
{
try
{
File.Copy(OrignFile, NewFile, true);
}
catch (Exception)
{
TextError = "error";
}
}
/// <summary>
/// 删除
/// </summary>
internal void DeleteFile(string delFile)
{
try
{
File.Delete(delFile);
}
catch (Exception)
{
TextError = "error";
}
}
/// <summary>
/// 移动
/// </summary>
internal void MoveFile(string OrignFile, string NewFile)
{
try
{
File.Move(OrignFile, NewFile);
}
catch (Exception)
{
}
}
#endregion
#region 文件夹类
/// <summary>
/// 处理创建文件夹,创建单的
/// </summary>
/// <param name="dname"></param>
/// <param name="dfile"></param>
/// <param name="iffor">是否要循环一条龙创建</param>
internal bool CreateFiler(string LocSrc, string FilerName,bool iffor)
{
if (iffor)
{
try
{
DirectoryInfo d = Directory.CreateDirectory(string.Concat(LocSrc,"\\", FilerName));
return true;
}
catch (Exception)
{
string thispath = LocSrc.Substring(0, LocSrc.LastIndexOf('\\'));
string folername = LocSrc.Substring(LocSrc.LastIndexOf('\\') + 1);
folername.Replace("\\", "");
while (CreateFiler(thispath, folername, true))
{
break;
}
//CreateFiler(LocSrc.Substring(0, LocSrc.LastIndexOf('\\')) + "\\", LocSrc.Substring(LocSrc.LastIndexOf('\\') + 1),true);
return true;
}
}
else
{
try
{
DirectoryInfo d = Directory.CreateDirectory(string.Concat(LocSrc, FilerName));
return true;
}
catch (Exception)
{
return false;
}
}
}
/// <summary>
/// 处理创建文件夹,创建单的
/// </summary>
/// <param name="dname"></param>
/// <param name="dfile"></param>
internal bool CreateFiler(string LocSrc, string FilerName)
{
try
{
DirectoryInfo d = Directory.CreateDirectory(string.Concat(LocSrc, FilerName));
return true;
}
catch (Exception)
{
CreateFiler(LocSrc, FilerName, true);
return false;
}
}
/// <summary>
/// 处理创建文件夹,处理几重的文件夹(直线创建)
/// </summary>
/// <param name="dname">c:\\式</param>
/// <param name="dfile"></param>
internal bool CreateFiler(string LocSrc, string[] ParmFilerName)
{
string src = LocSrc;
try
{
foreach (string s in ParmFilerName)
{
src = string.Concat(src, "\\", s);
Directory.CreateDirectory(src);
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 处理创建文件夹,处理几重的文件夹(分式创建)
/// </summary>
/// <param name="dname"></param>
/// <param name="dfile"></param>
internal bool CreateFiler(string LocSrc, string BaseFilerName, string[] ParmFilerName)
{
try
{
string src = string.Concat(LocSrc, "\\", BaseFilerName);
Directory.CreateDirectory(src);
Directory.SetCurrentDirectory(src);
foreach (string s in ParmFilerName)
{
Directory.CreateDirectory(s);
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 处理创建文件夹,处理几重的文件夹(多重直分式创建)
/// </summary>
/// <param name="dname"></param>
/// <param name="dfile"></param>
internal bool CreateFiler(string LocSrc, List<string> BaseFilerName, List<string[]> ParmFilerName)
{
try
{
for (int i = 0; i < BaseFilerName.Count; i++)
{
CreateFiler(LocSrc, BaseFilerName[i], ParmFilerName[i]);
}
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 递归删除文件夹及文件
/// </summary>
/// <param name="dir"></param>
internal void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
File.Delete(d); //直接删除其中的文件
else
DeleteFolder(d); //递归删除子文件夹
}
Directory.Delete(dir); //删除已空文件夹
TextError = dir + " 文件夹删除成功";
}
else
TextError = dir + " 该文件夹不存在"; //如果文件夹不存在则提示
}
/// <summary>
/// // ======================================================
/// 实现一个静态方法将指定文件夹下面的所有内容copy到目标文件夹下面
/// 如果目标文件夹为只读属性就会报错。
/// ======================================================
/// </summary>
/// <param name="srcPath"></param>
/// <param name="aimPath"></param>
internal void CopyDir(string srcPath, string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(aimPath)) Directory.CreateDirectory(aimPath);
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
// 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
// string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = Directory.GetFileSystemEntries(srcPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (Directory.Exists(file))
CopyDir(file, aimPath + Path.GetFileName(file));
// 否则直接Copy文件
else
File.Copy(file, aimPath + Path.GetFileName(file), true);
}
}
catch (Exception e)
{
TextError = e.ToString();
}
}
/// <summary>
/// // ======================================================
/// 实现一个静态方法将指定文件夹下面的所有内容Detele
/// 测试的时候要小心操作,删除之后无法恢复。
/// ======================================================
/// </summary>
/// <param name="aimPath"></param>
internal void DeleteDir(string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
// 如果你指向Delete目标文件下面的文件而不包含目录请使用下面的方法
// string[] fileList = Directory.GetFiles(aimPath);
string[] fileList = Directory.GetFileSystemEntries(aimPath);
// 遍历所有的文件和目录
foreach (string file in fileList)
{
// 先当作目录处理如果存在这个目录就递归Delete该目录下面的文件
if (Directory.Exists(file))
{
DeleteDir(aimPath + Path.GetFileName(file));
}
// 否则直接Delete文件
else
{
File.Delete(aimPath + Path.GetFileName(file));
}
}
//删除文件夹
System.IO.Directory.Delete(aimPath, true);
}
catch (Exception e)
{
TextError = e.ToString();
}
}
/// <summary>
/// 拷贝文件夹(包括子文件夹)到指定文件夹下,源文件夹和目标文件夹均需绝对路径. 格式: CopyFolder(源文件夹,目标文件夹);
/// </summary>
/// <param name="strFromPath"></param>
/// <param name="strToPath"></param>
internal void CopyFolder(string strFromPath, string strToPath)
{
//如果源文件夹不存在,则创建
if (!Directory.Exists(strFromPath))
{
Directory.CreateDirectory(strFromPath);
}
//取得要拷贝的文件夹名
string strFolderName = strFromPath.Substring(strFromPath.LastIndexOf("\\") + 1, strFromPath.Length - strFromPath.LastIndexOf("\\") - 1);
//如果目标文件夹中没有源文件夹则在目标文件夹中创建源文件夹
if (!Directory.Exists(strToPath + "\\" + strFolderName))
{
Directory.CreateDirectory(strToPath + "\\" + strFolderName);
}
//创建数组保存源文件夹下的文件名
string[] strFiles = Directory.GetFiles(strFromPath);
//循环拷贝文件
for (int i = 0; i < strFiles.Length; i++)
{
//取得拷贝的文件名,只取文件名,地址截掉。
string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("\\") + 1, strFiles[i].Length - strFiles[i].LastIndexOf("\\") - 1);
//开始拷贝文件,true表示覆盖同名文件
File.Copy(strFiles[i], strToPath + "\\" + strFolderName + "\\" + strFileName, true);
}
//创建DirectoryInfo实例
DirectoryInfo dirInfo = new DirectoryInfo(strFromPath);
//取得源文件夹下的所有子文件夹名称
DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
for (int j = 0; j < ZiPath.Length; j++)
{
//获取所有子文件夹名
string strZiPath = strFromPath + "\\" + ZiPath[j].ToString();
//把得到的子文件夹当成新的源文件夹,从头开始新一轮的拷贝
CopyFolder(strZiPath, strToPath + "\\" + strFolderName);
}
}
/// <summary>
/// 查找是否有这个文件夹
/// </summary>
/// <param name="strPath">路径</param>
/// <param name="folderName">文件夹名</param>
/// <returns></returns>
internal bool QueryFolderIt(string strPath, string folderName)
{
try
{
DirectoryInfo dires = new DirectoryInfo(strPath);
FileInfo[] files = dires.GetFiles();
foreach (FileInfo f in files)
{
if (f.Name.ToLower() == folderName.ToLower())
return true;//yield return 是要返回这个 IEnumerable<T>
}
return false;
}
catch (Exception)
{
//出错一般是没有文件夹
CreateFiler(strPath.Substring(0, strPath.LastIndexOf('\\'))+"\\", strPath.Substring(strPath.LastIndexOf('\\') + 1));
return false;
}
}
#endregion
}