using CnasSynchronousCommon;
using CnasSynchrousModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace CnasSynchronusClient
{
public class RemoteFileCopy
{
public InstrumentDataSourceInfo SyncInstrumentDSInfo { get; set; }
public RemoteFileCopy(InstrumentDataSourceInfo SyncInstrumentDSInfo)
{
this.SyncInstrumentDSInfo = SyncInstrumentDSInfo;
}
public bool isExistFile(string strPath)
{
if (File.Exists(strPath))
return true;
else
return false;
}
public void CopyFileFromRemote(string strPathSuffix,string stAutoFileName=null)
{
if (SyncInstrumentDSInfo.Path == null) return;
string[] strRemoteHostMsgs=SyncInstrumentDSInfo.Path.Split(new char[1] { '\\'}, StringSplitOptions.RemoveEmptyEntries);
if (strRemoteHostMsgs.Length > 0)
{
AppLog.Error("CopyFileFromRemote打印日志 "+ SyncInstrumentDSInfo.DsPathType);
AppLog.Error("CopyFileFromRemote打印日志 " + PathType.Remote);
AppLog.Error("CopyFileFromRemote打印日志 " + SyncInstrumentDSInfo.RemoteUser);
AppLog.Error("CopyFileFromRemote打印日志 " + SyncInstrumentDSInfo.RemoteUser);
AppLog.Error("CopyFileFromRemote打印日志 " + strRemoteHostMsgs[0]);
//执行net user,否则没有权限
if (SyncInstrumentDSInfo.DsPathType == PathType.Remote)
Connect(@"\\"+strRemoteHostMsgs[0], SyncInstrumentDSInfo.RemoteUser, SyncInstrumentDSInfo.RemoteUser);
AppLog.Error("CopyFileFromRemote打印日志 " + PathType.Remote);
//执行复制
string strPath = SyncInstrumentDSInfo.Path;
string strCatchPath = FileHelper.getBasePath() + "\\Cache\\";
string strCopyPath = System.Text.RegularExpressions.Regex.Replace(SyncInstrumentDSInfo.Path, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strPathSuffix;
if (stAutoFileName != null)
{
if(Path.GetExtension(strPath)=="")
strPath = strPath + "\\" + stAutoFileName;
strCopyPath = System.Text.RegularExpressions.Regex.Replace(strPath, "[ \\[ \\] \\^ \\-_*×――(^)|'$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").Replace(@"\", "") + strPathSuffix;
if (File.Exists(strCatchPath+strCopyPath))
File.Delete(strCatchPath+strCopyPath);
}
Transport(strPath, strCatchPath, strCopyPath);
}
else
{
AppLog.Error("未能正确获得解析后的远程主机名");
}
}
public static void DeleteDir(string file)
{
try
{
//去除文件夹和子文件的只读属性
//去除文件夹的只读属性
System.IO.DirectoryInfo fileInfo = new DirectoryInfo(file);
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
//去除文件的只读属性
System.IO.File.SetAttributes(file, System.IO.FileAttributes.Normal);
//判断文件夹是否还存在
if (Directory.Exists(file))
{
foreach (string f in Directory.GetFileSystemEntries(file))
{
if (File.Exists(f))
{
//如果有子文件删除文件
File.Delete(f);
Console.WriteLine(f);
}
else
{
//循环递归删除子文件夹
DeleteDir(f);
}
}
//删除空文件夹
Directory.Delete(file);
}
}
catch (Exception ex) // 异常处理
{
AppLog.Error(ex.Message);// 异常信息
}
}
public bool Connect(string remoteHostWithShareDirectory, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = "net use " + remoteHostWithShareDirectory + " " + passWord + " /User:" + userName;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (String.IsNullOrEmpty(errormsg))
{
Flag = true;
}
}
catch (Exception ex)
{
AppLog.Error(ex.Message);
throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
///
/// 向远程文件夹保存本地内容,或者从远程文件夹下载文件到本地
///
/// 要保存的文件的路径,如果保存文件到共享文件夹,这个路径就是本地文件路径如:@"D:\1.avi"
/// 保存文件的路径,不含名称及扩展名
/// 保存文件的名称以及扩展名
public static void Transport(string src, string dst, string fileName)
{
AppLog.Error("CopyFileFromRemote src 打印日志 " + src);
AppLog.Error("CopyFileFromRemote dst 打印日志 " + dst);
AppLog.Error("CopyFileFromRemote fileName 打印日志 " + fileName);
try
{
FileStream inFileStream = new FileStream(src, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
if (!Directory.Exists(dst))
{
Directory.CreateDirectory(dst);
}
dst = dst + fileName;
AppLog.Error("CopyFileFromRemote dst 打印日志 " + dst);
FileStream outFileStream = new FileStream(dst, FileMode.OpenOrCreate);
byte[] buf = new byte[inFileStream.Length];
int byteCount;
while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
{
outFileStream.Write(buf, 0, byteCount);
}
inFileStream.Flush();
inFileStream.Close();
outFileStream.Flush();
outFileStream.Close();
}
catch (Exception ex)
{
AppLog.Error(ex.Message);
}
}
}
}