///// <summary> ///// 从本地配置文件ftp_yj.ini中获取ftpIP ///// </summary> ///// <returns></returns> //public static string getFtpIP() //{ // string path = DBUtility.AppTempFilePath + "\\ftp_yj.ini"; // if (File.Exists(path)) // { // //读取文件内容 // FileStream fs = new FileStream(path, FileMode.Open); // StreamReader sr = new StreamReader(fs, Encoding.Default); // FtpServerIP = sr.ReadToEnd(); // sr.Close(); // } // return FtpServerIP; //} /// <summary> /// 获取上传地址 /// </summary> /// <param name="ftpServerIP">ftpIP</param> /// <param name="ftpServerDirectoryName">父目录</param> /// <returns>包括子目录的上传地址</returns> public static string getUploadUri() { //得到服务器一级目录的路径 string TopUploadURI = "ftp://" + FtpServerIP + "/"; //判断ftp目录信息 clsFTP cf = new clsFTP(new Uri(TopUploadURI + FtpServerDirectoryName + "/"), FtpUser, FtpPassword); string year = DateTime.Now.Year.ToString(); string month = DateTime.Now.Month.ToString(); if (month.Length == 1) { month = "0" + month; } string subDirectory = year + month; //判断子目录是否存在 if (!cf.DirectoryExist(subDirectory)) { cf.MakeDirectory(subDirectory); } string uploadURI = TopUploadURI + FtpServerDirectoryName + "/" + subDirectory + "/"; return(uploadURI); }
/// <summary> /// 删除ftp中的文件 /// </summary> /// <param name="ftpPath">ftp目录路径</param> /// <param name="remoteFileName">远程文件名</param> public static void DeleteFile(string ftpPath, string remoteFileName) { if (PublicClass.IsRegister && !UIPublicClass.PublicClass.IsSingleClent) { if (UIPublicClass.PublicClass.IsFTP) { #region 本地存储 //获取ftpIP if (FtpServerIP == string.Empty) { //if (getFtpIP() == string.Empty) //{ MessageBox.Show("未能获取连接原件的配置信息\t\n\t\n删除远程文件失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; //} } if (ftpPath != string.Empty && remoteFileName != string.Empty) { string FtpPath = TopUploadURI + ftpPath; //ftp的路径 string FtpFileName = remoteFileName; //ftp中保存的文件的名称 try { clsFTP cf = new clsFTP(new Uri(FtpPath), FtpUser, FtpPassword); bool ishave = cf.FileExist(FtpFileName); if (ishave) { cf.DeleteFile(FtpFileName); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } #endregion } else { #region 从数据库删除原件 UIPublicClass.UpLoadDownLoad.DeleteYj(Convert.ToInt32(ftpPath)); #endregion } } else { #region 本地存储 if (ftpPath != string.Empty && remoteFileName != string.Empty) { //string address = DBUtility.AppTempFilePath + "\\" + ftpPath + remoteFileName; string address = UIPublicClass.UpLoadDownLoad.CustomDirectory + "\\" + ftpPath + remoteFileName; if (File.Exists(address)) { File.Delete(address); } } #endregion } }
/// <summary> /// 上传原件 /// </summary> /// <param name="LocalFilePath">本地文件路径(含文件名)</param> /// <param name="LocalFileName">本地文件名称</param> /// <param name="LocalFileExt">本地文件扩展名</param> /// <param name="RemoteFileName">上传后保存的文件名</param> /// <param name="UploadURI">上传路径(不包含文件名)</param> /// <returns></returns> public static bool UpLoadFile(string LocalFilePath, ref string LocalFileName, ref string LocalFileExt, ref string RemoteFileName, ref string UploadURI) { if (UIPublicClass.PublicClass.IsRegister && !UIPublicClass.PublicClass.IsSingleClent) { //本地路径和文件名称信息 LocalFileName = LocalFilePath.Substring(LocalFilePath.LastIndexOf("\\") + 1); LocalFileExt = LocalFileName.Substring(LocalFileName.LastIndexOf(".") + 1); //存放到服务器中的文件名称 RemoteFileName = getNewFileName(LocalFileName); if (UIPublicClass.PublicClass.IsFTP) { #region 至FTP //获取ftpIP if (FtpServerIP == string.Empty) { //if (getFtpIP() == string.Empty) //{ MessageBox.Show("未能获取上传原件的配置信息\t\n\t\n上传原件失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); //} } try { UploadURI = getUploadUri(); //获取上传路径 clsFTP cf = new clsFTP(new Uri(UploadURI), FtpUser, FtpPassword); //初始化上传类 bool upload = cf.UploadFile(LocalFilePath, RemoteFileName); //上传原件 return(upload); } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } #endregion } else { #region 存储至数据库 int id = UIPublicClass.UpLoadDownLoad.CreateYj(LocalFilePath); UploadURI = TopUploadURI + id.ToString(); return((id == 0) ? false : true); #endregion } } else { #region 本地存储 //本地路径和文件名称信息 LocalFileName = LocalFilePath.Substring(LocalFilePath.LastIndexOf("\\") + 1); LocalFileExt = LocalFileName.Substring(LocalFileName.LastIndexOf(".") + 1); //存放到服务器中的文件名称 RemoteFileName = getNewFileName(LocalFileName); //获取上传路径 UploadURI = "ArchivesVideo\\"; //string address = DBUtility.AppTempFilePath + "\\" + UploadURI; string address = UIPublicClass.UpLoadDownLoad.CustomDirectory + "\\" + UploadURI; if (!Directory.Exists(address)) { Directory.CreateDirectory(address); } address = address + RemoteFileName; if (File.Exists(LocalFilePath)) { File.Copy(LocalFilePath, address, true); Boolean p = File.Exists(address);//查找文件是否存在 if (p) { return(true); } } else { //MessageBox.Show("未能获取原件信息!", // "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(false); #endregion } }