| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- namespace UAS_MES_NEW.PublicMethod
- {
- internal class BgFtpOperater
- {
- private string ftpServerIP;
- private string ftpUser;
- private string ftpPwd;
- public BgFtpOperater()
- {
- //百岗ftp
- string[] FTPInf = "ftp://10.8.0.208:21//mes/TestData|vsftpd|Az789***".Split('|');
- this.ftpServerIP = FTPInf[0];
- this.ftpUser = FTPInf[1];
- this.ftpPwd = FTPInf[2];
- //string FTPInf = Properties.Settings.Default.Properties["FTPAddress"].DefaultValue.ToString();
- //连接共享文件夹
- //status = BaseUtil.connectState(FTPInf);
- }
- public string UpLoadFile(string filepath, string filename, string UploadFolder)
- {
- try
- {
- FtpWebRequest reqFTP;
- if (FtpCheckDirectoryExist(UploadFolder) == "")
- {
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + UploadFolder + filename));
- reqFTP.UseBinary = true;
- reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
- reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
- FileInfo file = new FileInfo(filepath + "/" + filename);
- const int BufferSize = 2048;
- byte[] content = new byte[BufferSize - 1 + 1];
- int dataRead;
- using (FileStream fs = file.OpenRead())
- {
- //把上传的文件写入流
- using (Stream rs = reqFTP.GetRequestStream())
- {
- do
- {
- //每次读文件流的2KB
- dataRead = fs.Read(content, 0, BufferSize);
- rs.Write(content, 0, dataRead);
- } while (!(dataRead < BufferSize));
- rs.Close();
- }
- fs.Close();
- fs.Dispose();
- }
- file.Delete();
- }
- //Thread.Sleep(1000);
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- return "";
- //File.Delete(filepath + "/" + filename);
- }
- public string FtpCheckDirectoryExist(string destFilePath)
- {
- string fullDir = FtpParseDirectory(destFilePath);
- string[] dirs = fullDir.Split('/');
- string curDir = "/";
- for (int i = 0; i < dirs.Length; i++)
- {
- string dir = dirs[i];
- //如果是以/开始的路径,第一个为空
- if (dir != null && dir.Length > 0)
- {
- try
- {
- curDir += dir + "/";
- FtpMakeDir(curDir);
- }
- catch (Exception ex)
- {
- }
- }
- }
- return "";
- }
- public string FtpParseDirectory(string destFilePath)
- {
- return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
- }
- public Boolean FtpMakeDir(string localFile)
- {
- //Console.WriteLine(ftpServerIP + localFile);
- FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpServerIP + localFile);
- try
- {
- req.Credentials = new NetworkCredential(ftpUser, ftpPwd);
- req.Method = WebRequestMethods.Ftp.MakeDirectory;
- FtpWebResponse response = (FtpWebResponse)req.GetResponse();
- response.Close();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- req.Abort();
- return false;
- }
- req.Abort();
- return true;
- }
- }
- }
|