//备份到2号服务器 private void vf_Ftp2(object a) { Tools.WriteLog("Log", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "进入FTP2"); //检查是否有未上传的文件 while (!ib_Ftp2Upload) { bool OK = false; //List<string> list = INIFile.ReadKeyValues("IfFtpUpLoad"); List <string> list = SysVisitor.Current.ReadKeyValues("IfFtpUpLoad"); List <string> ls_Ftplist = ListToUploadList(list, "Ftp2");//获取待上传文件列表 if (ls_Ftplist.Count > 0) { OK = true; } //有文件未上传 //获取FTP2相关参数 string IP = SysVisitor.Current.of_GetMySysSet("FTP2", "IP"); string UserID = SysVisitor.Current.of_GetMySysSet("FTP2", "UserID"); string Password = SysVisitor.Current.of_GetMySysSet("FTP2", "Password"); string Path = SysVisitor.Current.of_GetMySysSet("FTP2", "Path"); string Port = SysVisitor.Current.of_GetMySysSet("FTP2", "Port"); FtpWeb Ftp = new FtpWeb(IP, Path, UserID, Password); if (!Ftp.Connect())//FTP 不可用 { ib_Ftp2Upload = true; Tools.WriteLog("error", "FTP2服务器不可用"); break; } if (OK) { if (!Ftp.DirectoryExist(DateTime.Now.ToString("yyyy-MM-dd"))) { Ftp.MakeDir(DateTime.Now.ToString("yyyy-MM-dd")); } Path = Path + @"\" + DateTime.Now.ToString("yyyy-MM-dd"); //FTP2文件上传 FtpWeb ftp = new FtpWeb(IP, Path, UserID, Password); int value = 0;//计算上传成功总数 for (int i = 0; i < ls_Ftplist.Count; i++) { int j = ftp.Upload(ls_Ftplist[i].ToString()); if (j == 1)//上传成功 { value++; } string str = SysVisitor.Current.of_GetMySysSet("IfFtpUpLoad", ls_Ftplist[i].ToString()); str = str.Substring(str.Length - 3, 1); SysVisitor.Current.of_SetMySysSet("IfFtpUpLoad", ls_Ftplist[i].ToString(), str + ",0");//更新节点 string ls_sql = "delete from mysysset where itemtype='IfFtpUpLoad' and itemvalue='0,0'"; MyConfig.ExecuteNonQuery(ls_sql); } Tools.WriteLog("Ftp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "FTP2已成功上传 " + value + " 个文件,剩余待上传个数:" + (ls_Ftplist.Count - value)); } Thread.Sleep(60 * 1000);//等待60秒 } return; } //Getreport
private void button9_Click(object sender, EventArgs e)//测试FTP2连接 { FtpWeb ftp = new FtpWeb(textBox13.Text.Trim(), textBox9.Text.Trim(), textBox12.Text.Trim(), textBox11.Text.Trim()); if (ftp.Connect() != true) { MessageBox.Show("成功连接FTP服务器"); } else { MessageBox.Show("连接失败,请检查用户名密码及路径"); } }
private void button8_Click(object sender, EventArgs e)//测试FTP1连接 { FtpWeb ftp = new FtpWeb(textBox4.Text.Trim(), textBox8.Text.Trim(), textBox5.Text.Trim(), textBox6.Text.Trim()); if (ftp.Connect() == true) { MessageBox.Show("成功连接FTP服务器1"); } else { MessageBox.Show("连接失败,请检查用户名密码及路径"); } }
//备份到2号服务器 private void vf_Ftp2(object a) { Tools.WriteLog("Log", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "进入FTP2"); //检查是否有未上传的文件 while (!ib_Ftp2Upload) { bool OK = false; //List<string> list = INIFile.ReadKeyValues("IfFtpUpLoad"); List<string> list = SysVisitor.Current.ReadKeyValues("IfFtpUpLoad"); List<string> ls_Ftplist = ListToUploadList(list, "Ftp2");//获取待上传文件列表 if (ls_Ftplist.Count > 0) { OK = true; }//有文件未上传 //获取FTP2相关参数 string IP = SysVisitor.Current.of_GetMySysSet("FTP2", "IP"); string UserID = SysVisitor.Current.of_GetMySysSet("FTP2", "UserID"); string Password = SysVisitor.Current.of_GetMySysSet("FTP2", "Password"); string Path = SysVisitor.Current.of_GetMySysSet("FTP2", "Path"); string Port = SysVisitor.Current.of_GetMySysSet("FTP2", "Port"); FtpWeb Ftp = new FtpWeb(IP, Path, UserID, Password); if (!Ftp.Connect())//FTP 不可用 { ib_Ftp2Upload = true; Tools.WriteLog("error", "FTP2服务器不可用"); break; } if (OK) { if (!Ftp.DirectoryExist(DateTime.Now.ToString("yyyy-MM-dd"))) { Ftp.MakeDir(DateTime.Now.ToString("yyyy-MM-dd")); } Path = Path + @"\" + DateTime.Now.ToString("yyyy-MM-dd"); //FTP2文件上传 FtpWeb ftp = new FtpWeb(IP, Path, UserID, Password); int value = 0;//计算上传成功总数 for (int i = 0; i < ls_Ftplist.Count; i++) { int j = ftp.Upload(ls_Ftplist[i].ToString()); if (j == 1)//上传成功 { value++; } string str = SysVisitor.Current.of_GetMySysSet("IfFtpUpLoad", ls_Ftplist[i].ToString()); str = str.Substring(str.Length - 3, 1); SysVisitor.Current.of_SetMySysSet("IfFtpUpLoad", ls_Ftplist[i].ToString(), str + ",0");//更新节点 string ls_sql = "delete from mysysset where itemtype='IfFtpUpLoad' and itemvalue='0,0'"; MyConfig.ExecuteNonQuery(ls_sql); } Tools.WriteLog("Ftp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "FTP2已成功上传 " + value + " 个文件,剩余待上传个数:" + (ls_Ftplist.Count - value)); } Thread.Sleep(60 * 1000);//等待60秒 } return; }
/// <summary> /// 删除订单目录 /// </summary> /// <param name="ftpServerIP">FTP 主机地址</param> /// <param name="folderToDelete">FTP 用户名</param> /// <param name="ftpUserID">FTP 用户名</param> /// <param name="ftpPassword">FTP 密码</param> public static void DeleteOrderDirectory(string ftpServerIP, string folderToDelete, string ftpUserID, string ftpPassword) { try{ if (!string.IsNullOrEmpty(ftpServerIP) && !string.IsNullOrEmpty(folderToDelete) && !string.IsNullOrEmpty(ftpUserID) && !string.IsNullOrEmpty(ftpPassword)) { FtpWeb fw = new FtpWeb(ftpServerIP, folderToDelete, ftpUserID, ftpPassword); //进入订单目录 fw.GotoDirectory(folderToDelete, true); //获取规格目录 string[] folders = fw.GetDirectoryList(); foreach (string folder in folders) { if (!string.IsNullOrEmpty(folder) || folder != "") { //进入订单目录 string subFolder = folderToDelete + "/" + folder; fw.GotoDirectory(subFolder, true); //获取文件列表 string[] files = fw.GetFileList("*.*"); if (files != null) { //删除文件 foreach (string file in files) { fw.Delete(file); } } //删除冲印规格文件夹 fw.GotoDirectory(folderToDelete, true); fw.RemoveDirectory(folder); } } //删除订单文件夹 string parentFolder = folderToDelete.Remove(folderToDelete.LastIndexOf('/')); string orderFolder = folderToDelete.Substring(folderToDelete.LastIndexOf('/') + 1); fw.GotoDirectory(parentFolder, true); fw.RemoveDirectory(orderFolder); } else { Insert_Standard_ErrorLog.Insert("FTP 及路径不能为空!"); } } catch(Exception ex) { Insert_Standard_ErrorLog.Insert("删除订单时发生错误,错误信息为:" + ex.Message); } }
/// <summary> /// 删除订单目录 /// </summary> /// <param name="ftpServerIP">FTP 主机地址</param> /// <param name="folderToDelete">FTP 用户名</param> /// <param name="ftpUserID">FTP 用户名</param> /// <param name="ftpPassword">FTP 密码</param> public static void DeleteOrderDirectory(string ftpServerIP, string folderToDelete, string ftpUserID, string ftpPassword) { try { if(!string.IsNullOrEmpty(ftpServerIP) && !string.IsNullOrEmpty(folderToDelete) && !string.IsNullOrEmpty(ftpUserID) && !string.IsNullOrEmpty(ftpPassword)) { FtpWeb fw = new FtpWeb(ftpServerIP, folderToDelete, ftpUserID, ftpPassword); //进入订单目录 fw.GotoDirectory(folderToDelete, true); //获取规格目录 string[] folders = fw.GetDirectoryList(); foreach(string folder in folders) { if(!string.IsNullOrEmpty(folder) || folder != "") { //进入订单目录 string subFolder = folderToDelete + "/" + folder; fw.GotoDirectory(subFolder, true); //获取文件列表 string[] files = fw.GetFileList("*.*"); if(files != null) { //删除文件 foreach(string file in files) { fw.Delete(file); } } //删除冲印规格文件夹 fw.GotoDirectory(folderToDelete, true); fw.RemoveDirectory(folder); } } //删除订单文件夹 string parentFolder = folderToDelete.Remove(folderToDelete.LastIndexOf('/')); string orderFolder = folderToDelete.Substring(folderToDelete.LastIndexOf('/') + 1); fw.GotoDirectory(parentFolder, true); fw.RemoveDirectory(orderFolder); } else { Insert_Standard_ErrorLog.Insert("FTP 及路径不能为空!"); } } catch(Exception ex) { Insert_Standard_ErrorLog.Insert("删除订单时发生错误,错误信息为:" + ex.Message); } }