天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

385 lines
17 KiB

using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using QMAPP.FJC.Entity.FileCopy;
using QMAPP.FJC.BLL.FileCopy;
namespace AutoFileCopyNet
{
/// <summary>
/// 功能描述:实现文件目录的文件复制功能,
/// 作者:sunguoqiang
/// 日期:2012-11-20
/// </summary>
public class DirHandler
{
/// <summary>
/// 复制文件,未做出错处理
/// </summary>
/// <param name="fromDir">源目录</param>
/// <param name="toDir">目标目录</param>
/// <param name="keyWords">文件夹名的查找关键字</param>
/// <param name="searchOption">查找方式,顶级目录或所有目录</param>
/// <param name="prefixFileName">文件重命名时使用的前缀,如果为空,相当于使用原名称</param>
public static void CopyDir(String fromDir, String toDir, String keyWords, SearchOption searchOption, String prefixFileName, String EQUIPMENT_Code, String EQUIPMENT_Type_Code, String localDir, string EQUIPMENT_KEY)
{
if (!System.IO.Directory.Exists(fromDir))
{
return;
}
FileCopyRecordBLL bll = new FileCopyRecordBLL();
FileCopyRecord record = new FileCopyRecord();
DirectoryInfo Dir = new DirectoryInfo(fromDir);
try
{
foreach (DirectoryInfo d in Dir.GetDirectories()) //查找所有子目录
{
string filename = d.ToString().Substring(0, 8); //获取日期
//文件日期小于当前日期的才拷贝(-1)
int i = filename.CompareTo(keyWords);
//如果不是当日
if (filename != keyWords && i < 0)
{
//从数据库读取子目录信息
List<FileCopyRecord> list = bll.GetAllList(new FileCopyRecord { RECORD_KEYWORDS = d.ToString(), EQUIPMENT_INFO_KEY = EQUIPMENT_KEY, FILE_HOSTNAME = Dns.GetHostName()});
//判断数据库中是否有记录,如果没有则插入数据
if (list.Count < 1)
{
CopyDir(d.FullName.ToString() + Path.DirectorySeparatorChar, toDir +Path.DirectorySeparatorChar+ d.ToString () + Path.DirectorySeparatorChar, prefixFileName);
#region "把文件复制记录写入数据库"
record.RECORD_KEY = Guid.NewGuid().ToString();
record.RECORD_EQUIPMENT_CODE = EQUIPMENT_Code;
record.RECORD_EQUIPMENT_TYPE_CODE = EQUIPMENT_Type_Code;
record.RECORD_EQUIPMENT_DIRECTORY = localDir;
record.RECORD_KEYWORDS = d.ToString();
record.FILE_HOSTNAME = Dns.GetHostName();
record.FILE_LASTWRITETIME = d.LastWriteTime;//.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo); ;
record.RECORD_COPY_DATETIME = DateTime.Now;
record.EQUIPMENT_INFO_KEY = EQUIPMENT_KEY;
bll.Insert(record);
#endregion
}
else if (!Directory.Exists(@toDir+"\\"+d.ToString() ))
{
CopyDir(d.FullName.ToString() + Path.DirectorySeparatorChar, toDir +Path.DirectorySeparatorChar+d.ToString() + Path.DirectorySeparatorChar, prefixFileName);
}
}
}
}
catch (Exception ex)
{
LogFileAccess.WriteLogFile("复制文件失败" + ex.Message + ex.Source + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
/// <summary>
/// 复制文件,未做出错处理
/// </summary>
/// <param name="fromDir">源目录</param>
/// <param name="toDir">目标目录</param>
/// <param name="prefixFileName">文件重命名时使用的前缀,如果为空,相当于使用原名称</param>
/// </summary>
public static void CopyDir(String fromDir, String toDir, String prefixFileName)
{
if (!System.IO.Directory.Exists(fromDir))
{
return;
}
if (toDir[toDir.Length - 1] != System.IO.Path.DirectorySeparatorChar)
{
toDir += System.IO.Path.DirectorySeparatorChar;
}
if (!System.IO.Directory.Exists(toDir))
{
System.IO.Directory.CreateDirectory(toDir);
}
DirectoryInfo Dir = new DirectoryInfo(fromDir);
try
{
String tempDir = String.Empty;
foreach (DirectoryInfo d in Dir.GetDirectories())//查找子目录
{
CopyDir(d.FullName.ToString() + Path.DirectorySeparatorChar, toDir + d.Name + Path.DirectorySeparatorChar, prefixFileName);
}
foreach (FileInfo f in Dir.GetFiles()) //查找文件
{
File.Copy(f.FullName, toDir + prefixFileName + f.Name, true);
}
}
catch (Exception ex)
{
LogFileAccess.WriteLogFile("复制文件失败" + ex.Message + ex.Source + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
/// <summary>
/// 复制文件,未做出错处理
/// </summary>
/// <param name="fromDir">源目录</param>
/// <param name="toDir">目标目录</param>
/// <param name="keyWords">文件夹名的查找关键字</param>
/// <param name="searchOption">查找方式,顶级目录或所有目录</param>
/// <param name="prefixFileName">文件重命名时使用的前缀,如果为空,相当于使用原名称</param>
/// <param name="lastWriteTime">文件的修改时间限制,只复制大于等于lastWriteTime的文件</param>
public static void CopyDir(String fromDir, String toDir, String keyWords, SearchOption searchOption, String prefixFileName, DateTime lastWriteTime)
{
if (!System.IO.Directory.Exists(fromDir))
{
return;
}
DirectoryInfo Dir = new DirectoryInfo(fromDir);
try
{
foreach (DirectoryInfo d in Dir.GetDirectories(keyWords, searchOption))//查找子目录
{
CopyDir(d.FullName.ToString() + Path.DirectorySeparatorChar, toDir + d.FullName.Substring(fromDir.Length) + Path.DirectorySeparatorChar, prefixFileName, lastWriteTime);
}
}
catch (Exception ex)
{
LogFileAccess.WriteLogFile("复制文件失败" + ex.Message + ex.Source + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
/// <summary>
/// 复制文件,未做出错处理
/// </summary>
/// <param name="fromDir">源目录</param>
/// <param name="toDir">目标目录</param>
/// <param name="prefixFileName">文件重命名时使用的前缀,如果为空,相当于使用原名称</param>
/// <param name="lastWriteTime">文件的修改时间限制,只复制大于等于lastWriteTime的文件</param>
public static void CopyDir(String fromDir, String toDir, String prefixFileName, DateTime lastWriteTime)
{
if (!System.IO.Directory.Exists(fromDir))
{
return;
}
if (toDir[toDir.Length - 1] != System.IO.Path.DirectorySeparatorChar)
{
toDir += System.IO.Path.DirectorySeparatorChar;
}
if (!System.IO.Directory.Exists(toDir))
{
System.IO.Directory.CreateDirectory(toDir);
}
DirectoryInfo Dir = new DirectoryInfo(fromDir);
try
{
String tempDir = String.Empty;
foreach (DirectoryInfo d in Dir.GetDirectories())//查找子目录
{
CopyDir(d.FullName.ToString() + Path.DirectorySeparatorChar, toDir + d.Name + Path.DirectorySeparatorChar, prefixFileName, lastWriteTime);
}
foreach (FileInfo f in Dir.GetFiles()) //查找文件
{
if (f.LastWriteTime >= lastWriteTime)
{
File.Copy(f.FullName, toDir + prefixFileName + f.Name, true);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
/// <summary>
/// 获取新文件名,格式为yyyyMMddHHmmss+4位随机数+"_"
/// </summary>
/// <returns>文件名</returns>
public static String GetNewPrefix()
{
Random r = new Random();
return DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + r.Next(1000, 9999) + "_";
}
/// <summary>
/// 复制远红外文件,未做出错处理
/// </summary>
/// <param name="fromDir">源目录</param>
/// <param name="toDir">目标目录</param>
public static void CopyDir(String fromDir, String toDir, String EQUIPMENT_Code, String EQUIPMENT_Type_Code, string EQUIPMENT_INFO_KEY)
{
FileCopyRecordBLL bll = new FileCopyRecordBLL();
FileCopyRecord record = new FileCopyRecord();
DateTime lastWriteTime = DateTime.Now;//当前日期
if (!System.IO.Directory.Exists(fromDir))
{
LogFileAccess.WriteLogFile("复制文件保存文件信息失败" + fromDir + "没有找到---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
return;
}
if (toDir[toDir.Length - 1] != System.IO.Path.DirectorySeparatorChar)
{
toDir += System.IO.Path.DirectorySeparatorChar;
}
if (!System.IO.Directory.Exists(toDir))
{
System.IO.Directory.CreateDirectory(toDir);
}
DirectoryInfo Dir = new DirectoryInfo(fromDir);
try
{
String tempDir = String.Empty;
foreach (DirectoryInfo d in Dir.GetDirectories())//查找子目录
{
CopyDir(d.FullName.ToString() + Path.DirectorySeparatorChar, toDir + d.Name + Path.DirectorySeparatorChar, EQUIPMENT_Code, EQUIPMENT_Type_Code, EQUIPMENT_INFO_KEY);
}
foreach (FileInfo f in Dir.GetFiles()) //查找文件
{
string hostname = Dns.GetHostName();
string lastwritetime = f.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
//查询该文件是否复制过,如果没有复制过,则开始复制该文件
List<FileCopyRecord> list = bll.GetAllList(new FileCopyRecord { RECORD_FILENAME = f.Name, EQUIPMENT_INFO_KEY = EQUIPMENT_INFO_KEY, FILE_HOSTNAME = hostname });
string templastwrit = "";
if (list.Count > 0)
{
record = list[0];
templastwrit = list[0].FILE_LASTWRITETIME.ToString();
}
if (list.Count <= 0)
{
File.Copy(f.FullName, toDir + f.Name, true);
#region "把文件复制记录写入数据库"
record.RECORD_KEY = Guid.NewGuid().ToString();
record.RECORD_EQUIPMENT_CODE = EQUIPMENT_Code;
record.RECORD_EQUIPMENT_TYPE_CODE = EQUIPMENT_Type_Code;
record.RECORD_EQUIPMENT_DIRECTORY = toDir;
record.RECORD_KEYWORDS = EQUIPMENT_Code;
record.RECORD_COPY_DATETIME = DateTime.Now;
record.RECORD_READFLAG = "0";
record.RECORD_FILENAME = f.Name;
record.EQUIPMENT_INFO_KEY = EQUIPMENT_INFO_KEY;
//record.FILE_LASTWRITETIME = lastwritetime;
record.FILE_HOSTNAME = hostname;
bll.Insert(record);
#endregion
}
else if (lastwritetime != templastwrit)
{
File.Copy(f.FullName, toDir + f.Name, true);//复制后覆盖原文件
#region "把修改数据库中文件复制时间"
record.RECORD_COPY_DATETIME = DateTime.Now;
//record.FILE_LASTWRITETIME = lastwritetime;
record.RECORD_FILENAME = f.Name;
record.FILE_HOSTNAME = Dns.GetHostName();
bll.Update(record);
#endregion
}
else if (!File.Exists(@toDir + f.Name))
{
File.Copy(f.FullName, toDir + f.Name, true);
}
}
}
catch(Exception ex)
{
LogFileAccess.WriteLogFile("复制文件保存文件信息失败" + ex.Message + ex.Source + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
/// <summary>
/// 复制远红外文件,未做出错处理
/// </summary>
/// <param name="fromDir">源目录</param>
/// <param name="toDir">目标目录</param>
public static void UpdateDir(List<FileStruct> files, String toDir, String EQUIPMENT_Code, String EQUIPMENT_Type_Code, string EQUIPMENT_INFO_KEY)
{
FileCopyRecordBLL bll = new FileCopyRecordBLL();
FileCopyRecord record = new FileCopyRecord();
DateTime lastWriteTime = DateTime.Now;//当前日期
try
{
String tempDir = String.Empty;
string hostname = Dns.GetHostName();
foreach (var item in files)
{
string lastwritetime = item.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
//查询该文件是否复制过,如果没有复制过,则开始复制该文件
List<FileCopyRecord> list = bll.GetAllList(new FileCopyRecord { RECORD_FILENAME = item.Name, EQUIPMENT_INFO_KEY = EQUIPMENT_INFO_KEY, FILE_HOSTNAME = hostname });
string templastwrit = "";
if (list.Count > 0)
{
record = list[0];
templastwrit = list[0].FILE_LASTWRITETIME.ToString();
}
if (list.Count <= 0)
{
#region "把文件复制记录写入数据库"
record.RECORD_KEY = Guid.NewGuid().ToString();
record.RECORD_EQUIPMENT_CODE = EQUIPMENT_Code;
record.RECORD_EQUIPMENT_TYPE_CODE = EQUIPMENT_Type_Code;
record.RECORD_EQUIPMENT_DIRECTORY = toDir;
record.RECORD_KEYWORDS = EQUIPMENT_Code;
record.RECORD_COPY_DATETIME = DateTime.Now;
record.RECORD_READFLAG = "0";
record.RECORD_FILENAME = item.Name;
record.EQUIPMENT_INFO_KEY = EQUIPMENT_INFO_KEY;
//record.FILE_LASTWRITETIME = lastwritetime;
record.FILE_HOSTNAME = hostname;
bll.Insert(record);
#endregion
}
else if (lastwritetime != templastwrit)
{
#region "把修改数据库中文件复制时间"
record.RECORD_COPY_DATETIME = DateTime.Now;
//record.FILE_LASTWRITETIME = lastwritetime;
record.RECORD_FILENAME =item.Name;
record.FILE_HOSTNAME = Dns.GetHostName();
bll.Update(record);
#endregion
}
}
}
catch (Exception ex)
{
LogFileAccess.WriteLogFile("复制文件保存文件信息失败" + ex.Message + ex.Source + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
}