|
@@ -22,18 +22,18 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
|
|
|
public ftpOperater()
|
|
|
{
|
|
|
- //string[] FTPInf = Properties.Settings.Default.Properties["FTPAddress"].DefaultValue.ToString().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);
|
|
|
+ string[] FTPInf = Properties.Settings.Default.Properties["FTPAddress"].DefaultValue.ToString().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);
|
|
|
}
|
|
|
+
|
|
|
#region
|
|
|
- public void UpLoadFile(string filepath, string filename)
|
|
|
+ public void UpLoadFile(string filepath, string filename, string UploadFolder)
|
|
|
{
|
|
|
//上传之前判断文件是否存在
|
|
|
string[] filelist = GetFileList();
|
|
@@ -50,7 +50,8 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
}
|
|
|
}
|
|
|
FtpWebRequest reqFTP;
|
|
|
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
|
|
|
+ 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;
|
|
@@ -73,53 +74,54 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
}
|
|
|
fs.Close();
|
|
|
}
|
|
|
+ File.Delete(filepath + "/" + filename);
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
- public void UpLoadFile(string filepath, string filename,string savepath)
|
|
|
- {
|
|
|
- if (status)
|
|
|
- {
|
|
|
- //目标路径
|
|
|
- string targetPath = savepath;
|
|
|
- //var file = Directory.GetFiles(targetPath);
|
|
|
- string sourceFile = Path.Combine(filepath + @"\", filename);
|
|
|
- string destFile = Path.Combine(targetPath + @"\", filename);
|
|
|
- //获取指定路径下的全部文件名
|
|
|
- var file = Directory.GetFiles(targetPath);
|
|
|
- string overwrite = "";
|
|
|
- for (int i = 0; i < file.Length; i++)
|
|
|
- {
|
|
|
- if (file[i].Substring(file[i].LastIndexOf(@"\") + 1) == filename)
|
|
|
- {
|
|
|
- overwrite = MessageBox.Show("已存在名为" + filename + "的文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- if (overwrite == "Yes" || overwrite == "")
|
|
|
- {
|
|
|
- //不存在文件的话进行创建
|
|
|
- if (!Directory.Exists(targetPath))
|
|
|
- Directory.CreateDirectory(targetPath);
|
|
|
- //将文件复制到指定位置
|
|
|
- File.Copy(sourceFile, destFile, true);
|
|
|
- }
|
|
|
- //string sourceFile = Path.Combine(filepath+@"\", filename);
|
|
|
- ////共享文件夹的目录
|
|
|
- //DirectoryInfo theFolder = new DirectoryInfo(savepath);
|
|
|
- ////获取保存文件的路径
|
|
|
- //string fielpath = theFolder.ToString() + @"\";
|
|
|
- ////执行方法
|
|
|
- //BaseUtil.Transport(sourceFile, fielpath, filename);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //ListBox1.Items.Add("未能连接!");
|
|
|
- MessageBox.Show("共享文件连接错误");
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+ //public void UpLoadFile(string filepath, string filename, string savepath)
|
|
|
+ //{
|
|
|
+ // if (status)
|
|
|
+ // {
|
|
|
+ // //目标路径
|
|
|
+ // string targetPath = savepath;
|
|
|
+ // //var file = Directory.GetFiles(targetPath);
|
|
|
+ // string sourceFile = Path.Combine(filepath + @"\", filename);
|
|
|
+ // string destFile = Path.Combine(targetPath + @"\", filename);
|
|
|
+ // //获取指定路径下的全部文件名
|
|
|
+ // var file = Directory.GetFiles(targetPath);
|
|
|
+ // string overwrite = "";
|
|
|
+ // for (int i = 0; i < file.Length; i++)
|
|
|
+ // {
|
|
|
+ // if (file[i].Substring(file[i].LastIndexOf(@"\") + 1) == filename)
|
|
|
+ // {
|
|
|
+ // overwrite = MessageBox.Show("已存在名为" + filename + "的文件,是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // if (overwrite == "Yes" || overwrite == "")
|
|
|
+ // {
|
|
|
+ // //不存在文件的话进行创建
|
|
|
+ // if (!Directory.Exists(targetPath))
|
|
|
+ // Directory.CreateDirectory(targetPath);
|
|
|
+ // //将文件复制到指定位置
|
|
|
+ // File.Copy(sourceFile, destFile, true);
|
|
|
+ // }
|
|
|
+ // //string sourceFile = Path.Combine(filepath+@"\", filename);
|
|
|
+ // ////共享文件夹的目录
|
|
|
+ // //DirectoryInfo theFolder = new DirectoryInfo(savepath);
|
|
|
+ // ////获取保存文件的路径
|
|
|
+ // //string fielpath = theFolder.ToString() + @"\";
|
|
|
+ // ////执行方法
|
|
|
+ // //BaseUtil.Transport(sourceFile, fielpath, filename);
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // //ListBox1.Items.Add("未能连接!");
|
|
|
+ // MessageBox.Show("共享文件连接错误");
|
|
|
+ // }
|
|
|
+
|
|
|
+ //}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取ftp服务器上的文件信息
|
|
@@ -230,6 +232,54 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
return fileSize;
|
|
|
}
|
|
|
|
|
|
+ public void 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)
|
|
|
+ { }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public string FtpParseDirectory(string destFilePath)
|
|
|
+ {
|
|
|
+ return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建目录
|
|
|
+ public Boolean FtpMakeDir(string localFile)
|
|
|
+ {
|
|
|
+ FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpServerIP + localFile);
|
|
|
+ req.Credentials = new NetworkCredential(ftpUser, ftpPwd);
|
|
|
+ req.Method = WebRequestMethods.Ftp.MakeDirectory;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ FtpWebResponse response = (FtpWebResponse)req.GetResponse();
|
|
|
+ response.Close();
|
|
|
+ }
|
|
|
+ catch (Exception)
|
|
|
+ {
|
|
|
+ req.Abort();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ req.Abort();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 实现ftp下载操作
|
|
|
/// </summary>
|
|
@@ -271,7 +321,7 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
}
|
|
|
|
|
|
|
|
|
- public string DownLoadFromSharePath(string URL,string fileName)
|
|
|
+ public string DownLoadFromSharePath(string URL, string fileName)
|
|
|
{
|
|
|
//目标路径
|
|
|
string targetPath = FTPInf;
|
|
@@ -280,14 +330,15 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
string destFile = Path.Combine(DownLoadTo, fileName);
|
|
|
//不存在文件的话进行创建
|
|
|
if (!Directory.Exists(DownLoadTo))
|
|
|
- Directory.CreateDirectory(DownLoadTo);
|
|
|
+ Directory.CreateDirectory(DownLoadTo);
|
|
|
//将文件复制到指定位置
|
|
|
try
|
|
|
{
|
|
|
File.Copy(sourceFile, destFile, true);
|
|
|
}
|
|
|
- catch {
|
|
|
- MessageBox.Show("标签文件更新失败,不在指定维护路径"+ URL + "中或已被占用","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
|
|
|
+ catch
|
|
|
+ {
|
|
|
+ MessageBox.Show("标签文件更新失败,不在指定维护路径" + URL + "中或已被占用", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
|
}
|
|
|
return destFile;
|
|
|
}
|
|
@@ -298,30 +349,30 @@ namespace UAS_MES_NEW.PublicMethod
|
|
|
/// <param name="fileName"></param>
|
|
|
public void Delete(string fileName)
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- FtpWebRequest reqFTP;
|
|
|
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
|
|
|
- reqFTP.KeepAlive = false;
|
|
|
- reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ FtpWebRequest reqFTP;
|
|
|
+ reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + fileName));
|
|
|
+ reqFTP.KeepAlive = false;
|
|
|
+ reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
|
|
|
reqFTP.UseBinary = true;
|
|
|
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
|
|
|
reqFTP.UsePassive = true;
|
|
|
string result = String.Empty;
|
|
|
- FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
|
|
- long size = response.ContentLength;
|
|
|
- Stream datastream = response.GetResponseStream();
|
|
|
- StreamReader sr = new StreamReader(datastream);
|
|
|
- result = sr.ReadToEnd();
|
|
|
- sr.Close();
|
|
|
- datastream.Close();
|
|
|
- response.Close();
|
|
|
- //Buffer.Log(string.Format("Ftp文件{1}删除成功!", DateTime.Now.ToString(), fileName));
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- throw ex;
|
|
|
- }
|
|
|
- }
|
|
|
+ FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
|
|
+ long size = response.ContentLength;
|
|
|
+ Stream datastream = response.GetResponseStream();
|
|
|
+ StreamReader sr = new StreamReader(datastream);
|
|
|
+ result = sr.ReadToEnd();
|
|
|
+ sr.Close();
|
|
|
+ datastream.Close();
|
|
|
+ response.Close();
|
|
|
+ //Buffer.Log(string.Format("Ftp文件{1}删除成功!", DateTime.Now.ToString(), fileName));
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw ex;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|