public static void MoveFile(string fullpath, string fileName, string destdir) { try { if (!Directory.Exists(destdir)) { Directory.CreateDirectory(destdir); } string destpath = destdir + fileName; //目标路径 if (File.Exists(destpath)) { File.Delete(destpath); //目标目录中有此文件,则先删除,再移过来。 } File.Move(fullpath, destpath); UtilLog.Info("Move file: " + fullpath + " to path: " + destdir); } catch (Exception ex) { UtilLog.Error("Handle Move File Err:", ex); } }
public static bool CopyFile(string fullpath, string fileName, string destdir) { try { if (!Directory.Exists(destdir)) { Directory.CreateDirectory(destdir); } string destpath = destdir + fileName; //目标路径 if (File.Exists(destpath)) { File.Delete(destpath); //目标目录中有此文件,则先删除,再移过来。 } File.Copy(fullpath, destpath); return(true); } catch (Exception ex) { UtilLog.Error("Handle Copy File Err:", ex); return(false); } }
public static string DownloadRecipe(string ftpuri, string DownloadDir, string ext) { string uri = ftpuri.Split(',')[0]; string ftpUserID = ftpuri.Split(',')[1]; string ftpPassword = ftpuri.Split(',')[2]; string fileName = uri.Replace("/", "@").Split('@')[4].ToString() + "." + ext; uri = uri + "." + ext; FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri)); reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.Proxy = null; System.Text.StringBuilder strMsg; int bufferSize = 2048; byte[] buffer = new byte[bufferSize]; string targetFilePath = DownloadDir + "\\" + fileName; FileStream outputStream = null; FtpWebResponse response = null; Stream ftpStream = null; try { outputStream = new FileStream(targetFilePath, FileMode.Create); response = (FtpWebResponse)reqFTP.GetResponse(); ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } strMsg = new System.Text.StringBuilder("OK"); } catch (Exception ex) { string ErrStr = ex.ToString(); UtilLog.Error(ex); if (ErrStr.Substring(0, 67).Trim() == "System.Net.WebException: The remote server returned an error: (550)") { strMsg = new System.Text.StringBuilder("Test Program loading error!-程序调用错误!" + "\r\n"); strMsg.Append(fileName + " Program version is not correct!-程序版本号错误!" + "\r\n"); } else { strMsg = new System.Text.StringBuilder("Test Program loading error!-程序调用错误!" + "\r\n"); strMsg.Append(fileName + " Program file doesn't exist!-程序文件不存在!" + "\r\n"); strMsg.Append("Pls try again!-请重新扫描!" + "\r\n"); } if (ftpStream != null) { ftpStream.Close(); } if (outputStream != null) { outputStream.Close(); } if (response != null) { response.Close(); } } finally { if (ftpStream != null) { ftpStream.Close(); } if (outputStream != null) { outputStream.Close(); } if (response != null) { response.Close(); } } return(strMsg.ToString()); }