1. 程式人生 > >文件復制,遍歷,ftp下載

文件復制,遍歷,ftp下載

man src 添加 over inf dir iis eps ()

技術分享
public partial class WebPp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string path = Request.PhysicalApplicationPath + "WebPrep/PrepSetp/" +"aa.pdf";

            if (!File.Exists(path))
            {
                if (DownLoadPdf())//
PDF下載成功 { //將下載的pdf復制到IIS訪問目錄下 FileInfo file = new FileInfo(ConfigurationManager.AppSettings["FilePath"] + Request.QueryString["PdfName"]); FileInfo fileDest = new FileInfo(path); if (file.Exists) {
if (Directory.Exists(fileDest.DirectoryName) == false) { Directory.CreateDirectory(fileDest.DirectoryName); } else { DirectoryInfo fld = new DirectoryInfo(fileDest.DirectoryName); FileInfo[] fileInfo
= fld.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍歷文件 { try {//刪除訪問日期7天前的數據 if (NextFile.LastAccessTime < DateTime.Now.AddDays(-7)) { NextFile.Delete(); } } catch (Exception) { } } } file.CopyTo(path, true); } } } } catch (Exception ex) { } } /// <summary> /// 添加下載鎖 /// </summary> private static readonly object FTPDownLoad = new object(); private bool DownLoadPdf() { if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["FilePath"])|| string.IsNullOrEmpty(Request.QueryString["PdfName"])) return false; //實例化一個文件對象 FileInfo fileInfo = null; fileInfo = new FileInfo(ConfigurationManager.AppSettings["FilePath"] + Request.QueryString["PdfName"].ToString()); FtpClient ftp = new FtpClient(); //如果文件存在 if (fileInfo.Exists == true && fileInfo.Length > 0) return true; lock (FTPDownLoad) { //如果文件為空 try { string ftpServer = ConfigurationManager.AppSettings["LBFTPServer"]; string ftpPort = ConfigurationManager.AppSettings["LBFTPPort"]; string ftpUserName = ConfigurationManager.AppSettings["LBFTPUserName"]; string ftpPassword = ConfigurationManager.AppSettings["LBFTPPassword"]; ftp.Server = ftpServer; ftp.Port = int.Parse(ftpPort); ftp.Username = ftpUserName; ftp.Password = ftpPassword; ftp.ChangeDir(fileInfo.DirectoryName.Split(\\)[fileInfo.DirectoryName.Split(\\).Length - 1]); if (Directory.Exists(fileInfo.DirectoryName) == false) { Directory.CreateDirectory(fileInfo.DirectoryName); } ftp.Download(fileInfo.Name, fileInfo.FullName, true); return true; } catch (Exception ex) { SunWin.Common.CommonUtil.LogHelper.Error(Session["CurrentUser"].ToString(), GetClientIp.GetClientIP(Context), "附件通用下載頁,FTP方法", ex.Message); } finally { ftp.Close(); } } } /// <summary> /// 方法重寫,解決控件必須放在具有 runat=server 的窗體標記內 /// </summary> /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for } }   
View Code

文件復制,遍歷,ftp下載