using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; namespace Core.StlMes.Client.LgResMgt.Mcms { public class VSFTPHelper { #region 字段或属性 public string IP { get; set; } public string UserName { get; set; } public string Password { get; set; } public string FtpPath { get; set; } public bool Passive { get; set; } public bool Binary { get; set; } #endregion #region 构造 /// /// 构造 /// public VSFTPHelper( string ftpIp,string uid,string pwd , string path ) { IP = ftpIp; Passive = true; UserName = uid; Password = pwd; FtpPath = path; Binary = true; WriteLog(string.Format("VSFTP参数设定: {0} {1} {2}", IP, UserName, Password)); } #endregion #region 创建文件夹 /// /// 创建一个目录 /// public void CreateDirectory(string filesUrl) { try { Uri uri = new Uri("ftp://" + IP + FtpPath + filesUrl); List list = new List(); FtpWebRequest req = (FtpWebRequest)WebRequest.Create(uri); // req.Credentials = new NetworkCredential(UserName, Password); req.Method = WebRequestMethods.Ftp.MakeDirectory; req.UseBinary = Binary; FtpWebResponse res = (FtpWebResponse)req.GetResponse(); //GetFileList(filesUrl); //假如创建不成功,反复调用。。。 } catch (Exception exp) { } } /// /// 获取FTP文件列表 /jldata/ftppicture/ /// /// public List GetFileList(string filesUrl) { List list = new List(); try { FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + IP + FtpPath + "/" + filesUrl)); // req.Credentials = new NetworkCredential(UserName, Password); req.Method = WebRequestMethods.Ftp.ListDirectory; req.UseBinary = Binary; req.UsePassive = Passive; using (FtpWebResponse res = (FtpWebResponse)req.GetResponse()) { using (StreamReader sr = new StreamReader(res.GetResponseStream())) { string s; while ((s = sr.ReadLine()) != null) { list.Add(s); } } } //这里的list是文件夹下的文件信息 return list; } catch (Exception exp) { CreateDirectory(filesUrl); return list; } } #endregion #region 上传文件 /// /// FTP图片上传 /// /// 本地文件夹路径 public bool FtpUp(string localPath) { try { //创建文件夹 CreateDirectory(DateTime.Now.ToString("yyyy-MM-dd")); //上传文件 UploadFile(localPath); //未截图成功的,也上传一个图片 //File.Copy(strFilePath + "\\image\\null.jpg", strPathImage); //UploadFile(strPathImage); return true; } catch (Exception exp) { WriteLog("FTP图片上传失败!" + exp.Message); return false; } } private void UploadFile(string localPath) { FileInfo fi = new FileInfo(localPath); FileStream fs = fi.OpenRead(); long length = fs.Length; FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + IP + FtpPath + DateTime.Now.ToString("yyyy-MM-dd") + "/" + fi.Name); req.Credentials = new NetworkCredential(UserName, Password); req.Method = WebRequestMethods.Ftp.UploadFile; req.UseBinary = Binary; req.ContentLength = length; req.Timeout = 10 * 1000; Stream stream = req.GetRequestStream(); int BufferLength = 2048; //2K byte[] b = new byte[BufferLength]; int i; while ((i = fs.Read(b, 0, BufferLength)) > 0) { stream.Write(b, 0, i); } stream.Close(); stream.Dispose(); fs.Close(); } #endregion #region 写日志 private static void WriteLog(string str) { string m_szRunPath; try { m_szRunPath = System.Environment.CurrentDirectory; if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false) { System.IO.Directory.CreateDirectory(m_szRunPath + "\\log"); } string strDate = System.DateTime.Now.ToString("yyyyMMdd"); string strPathFile = m_szRunPath + "\\log\\" + strDate; if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹 { Directory.CreateDirectory(strPathFile); } System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\FTP_" + strDate + ".log", true); tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); tw.WriteLine(str); tw.WriteLine("\r\n"); tw.Close(); } catch { } } #endregion } }