1 public void Run() 2 { 3 //获取目标文件列表 4 string _ErrorMessage = ""; 5 string _ErrorMessageFile = "errorLog.txt"; 6 FileHelper fh = new FileHelper(); 7 string servername = "";//服务器地址 8 string username = "";//用户名 9 string password = "";//用户密码 10 string filePath = "";//源文件目录 11 string targetFilePath = "";//目标目录 12 13 string _configtxt = "configtxt.txt";//配置文件 14 string _foldertxt = "foldertxt.txt";//源文件及目标目录列表 15 16 try 17 { 18 //获取配置文件 19 string configRes = GetFileString(_configtxt); 20 21 if (configRes == "读取配置文件失败") 22 { 23 Console.WriteLine("读取配置文件失败"); 24 _ErrorMessage = "读取配置文件失败"; 25 WriteLog(_ErrorMessage, _ErrorMessageFile); 26 return; 27 } 28 //登录 29 Dictionary<string, string> dicconfig = GetDicFromStr(configRes); 30 try 31 { 32 servername = dicconfig["servername"]; 33 username = dicconfig["username"]; 34 password = dicconfig["password"]; 35 } 36 catch 37 { 38 Console.WriteLine("配置文件错误"); 39 _ErrorMessage = "配置文件错误"; 40 WriteLog(_ErrorMessage, _ErrorMessageFile); 41 return; 42 } 43 //开始读取源文件数据 44 string folderRes = GetFileString(_foldertxt); 45 Dictionary<string, string> dicFolders = GetDicFromStr(folderRes); 46 string _defaultfolder = "";//默认拷贝目录 47 foreach (var folderdata in dicFolders) 48 { 49 if (folderdata.Key == "defaultfolder") 50 { 51 _defaultfolder = folderdata.Value; 52 continue; 53 } 54 filePath = folderdata.Key;//源文件目录 55 56 string temptargetFilePath = folderdata.Value == "" ? _defaultfolder : folderdata.Value; 57 //获取源文件后2个目录名 58 string toFilePath = filePath.Split('\\')[filePath.Split('\\').Length - 2] + "\\" + filePath.Split('\\')[filePath.Split('\\').Length - 1]; 59 targetFilePath = temptargetFilePath.Trim('\\') + "\\" + toFilePath;//目标文件目录 60 if (!Directory.Exists(targetFilePath)) 61 { 62 Directory.CreateDirectory(targetFilePath); 63 } 64 65 //建立连接并且下载 66 if (FileHelper.connectState(servername, username, password)) 67 { 68 string[] fileArr = fh.readlist(filePath); 69 //剪切所有文件 70 foreach (string filename in fileArr) 71 { 72 try 73 { 74 fh.FileMove(filename, targetFilePath.Trim('\\') + "\\" + GetFileNameFromPath(filename)); 75 } 76 catch 77 { 78 Console.WriteLine(filename + " 文件下载失败"+DateTime.Now.ToString()); 79 } 80 } 81 } 82 83 } 84 85 } 86 catch(Exception ex) 87 { 88 Console.WriteLine(ex.Message + ex.Source + ex.TargetSite); 89 _ErrorMessage = ex.Message + ex.Source + ex.TargetSite; 90 } 91 if (_ErrorMessage != "") 92 { 93 WriteLog(_ErrorMessage, _ErrorMessageFile); 94 } 95 96 97 } 98 //写错误日志 99 private void WriteLog(string data,string logname) 100 { 101 try 102 { 103 FileMode fm = FileMode.Append; 104 if (!File.Exists(System.AppDomain.CurrentDomain.BaseDirectory+logname)) 105 { 106 fm = FileMode.CreateNew; 107 } 108 string configfilename = System.AppDomain.CurrentDomain.BaseDirectory + logname; 109 //读取文本文件 110 FileStream fs = new FileStream(configfilename, fm); 111 StreamWriter sw = new StreamWriter(fs, Encoding.Default); 112 113 sw.Write(DateTime.Now.ToString()+"---------"+ data); 114 115 //释放占用 116 sw.Close(); 117 sw.Dispose(); 118 fs.Close(); 119 fs.Dispose(); 120 } 121 catch 122 { 123 } 124 125 } 126 127 private Dictionary<string, string> GetDicFromStr(string sourcestr) 128 { 129 Dictionary<string, string> dicres = new Dictionary<string, string>(); 130 string[] datas = sourcestr.Replace("\r","").Split('\n'); 131 foreach (string data in datas) 132 { 133 if (string.IsNullOrEmpty(data)) 134 { 135 continue; 136 } 137 try 138 { 139 string[] dataarr = data.Split('='); 140 if (dataarr.Length == 2) 141 { 142 143 dicres.Add(dataarr[0], dataarr[1]); 144 } 145 else 146 { 147 dicres.Add(dataarr[0], ""); 148 } 149 } 150 catch 151 { 152 153 } 154 } 155 return dicres; 156 } 157 private string GetFileNameFromPath(string path) 158 { 159 return path.Split('\\')[path.Split('\\').Length - 1]; 160 } 161 private string GetFileString(string configname) 162 { 163 try 164 { 165 string configfilename = System.AppDomain.CurrentDomain.BaseDirectory + configname; 166 //读取文本文件 167 FileStream fs = new FileStream(configfilename, FileMode.Open); 168 StreamReader m_streamReader = new StreamReader(fs, Encoding.Default); 169 m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin); 170 //获取数据 171 string myFileStr = m_streamReader.ReadToEnd(); 172 173 //释放占用 174 m_streamReader.Close(); 175 m_streamReader.Dispose(); 176 fs.Close(); 177 fs.Dispose(); 178 179 return myFileStr; 180 } 181 catch 182 { 183 return "读取配置文件失败"; 184 } 185 }
Filehelper.cs内容
1 public class FileHelper 2 { 3 4 System.Collections.ArrayList alst; 5 6 7 //得到路径下所有文件(包括子文件夹下的文件) 8 public string[] readlist(string path) 9 { 10 alst = new System.Collections.ArrayList();//建立ArrayList对象 11 GetDirs(path);//得到文件夹 12 return (string[])alst.ToArray(typeof(string));//把ArrayList转化为string[] 13 } 14 15 public void GetFiles(string dir) 16 { 17 try 18 { 19 string[] files = Directory.GetFiles(dir);//得到文件 20 foreach (string file in files)//循环文件 21 { 22 string exname = file.Substring(file.LastIndexOf(".") + 1);//得到后缀名 23 // if (".txt|.aspx".IndexOf(file.Substring(file.LastIndexOf(".") + 1)) > -1)//查找.txt .aspx结尾的文件 24 //if (".txt".IndexOf(file.ToLower().Substring(file.LastIndexOf(".") + 1)) > -1)//如果后缀名为.txt文件 25 //{ 26 // FileInfo fi = new FileInfo(file);//建立FileInfo对象 27 // alst.Add(fi.FullName);//把.txt文件全名加人到FileInfo对象 28 //} 29 FileInfo fi = new FileInfo(file);//建立FileInfo对象 30 alst.Add(fi.FullName); 31 } 32 } 33 catch 34 { 35 36 } 37 } 38 39 public void GetDirs(string d)//得到所有文件夹 40 { 41 GetFiles(d);//得到所有文件夹里面的文件 42 try 43 { 44 string[] dirs = Directory.GetDirectories(d); 45 foreach (string dir in dirs) 46 { 47 GetDirs(dir);//递归 48 } 49 } 50 catch 51 { 52 } 53 } 54 55 //文件剪切 56 public void FileMove(string filePath, string toFilePath) 57 { 58 string dirPath = filePath; 59 string sourcePath = toFilePath; 60 FileInfo file = new FileInfo(dirPath); 61 file.MoveTo(sourcePath); 62 } 63 //建立共享文件连接 64 public static bool connectState(string path, string userName, string passWord) 65 { 66 bool Flag = false; 67 Process proc = new Process(); 68 try 69 { 70 proc.StartInfo.FileName = "cmd.exe"; 71 proc.StartInfo.UseShellExecute = false; 72 proc.StartInfo.RedirectStandardInput = true; 73 proc.StartInfo.RedirectStandardOutput = true; 74 proc.StartInfo.RedirectStandardError = true; 75 proc.StartInfo.CreateNoWindow = true; 76 proc.Start(); 77 string dosLine = @"net use " + path + " /user:" + userName + " " + passWord ; 78 proc.StandardInput.WriteLine(dosLine); 79 proc.StandardInput.WriteLine("exit"); 80 while (!proc.HasExited) 81 { 82 proc.WaitForExit(1000); 83 } 84 string errormsg = proc.StandardError.ReadToEnd(); 85 proc.StandardError.Close(); 86 if (string.IsNullOrEmpty(errormsg)) 87 { 88 Flag = true; 89 } 90 else 91 { 92 throw new Exception(errormsg); 93 } 94 } 95 catch (Exception ex) 96 { 97 throw ex; 98 } 99 finally 100 { 101 proc.Close(); 102 proc.Dispose(); 103 } 104 return Flag; 105 } 106 107 }