Open XML的上传、下载 、删除 ......文件路径

本博客介绍了一个用于获取下载站点的函数,并在指定不存在的临时文件夹中创建之。同时,提供了删除前一天创建的临时文件的功能,确保了文件夹只包含当天创建的文件。
 /// <summary>
        /// Get download site, if download tempfolder not existed, create it first
        /// </summary>
        /// <param name="filePath">the template file path</param>
        /// <returns>download path</returns>
        private string GetDownloadFilePath(string filePath)
        {
            string downloadFilePath = "";
            if (!string.IsNullOrEmpty(filePath))
            {
                string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                fileName = fileName.Substring(0, fileName.LastIndexOf(".")) + "_" + DateTime.Now.ToString("yyyyMMddHHmmssffff", System.Globalization.DateTimeFormatInfo.InvariantInfo);//add current time to the fileName
                downloadFilePath = Utility.GetAppSetting("TempDirectory_Download");
                //if download temp folder not existed, create it
                if (!Directory.Exists(downloadFilePath))
                {
                    Directory.CreateDirectory(downloadFilePath);
                }
                downloadFilePath += fileName + filePath.Substring(filePath.LastIndexOf("."));
            }
            return downloadFilePath;
        }
        /// <summary>
        /// delete the temp files which were not created by today
        /// </summary>
        /// <param name="filePath">the temp directory for download</param>
        private void DeletePreviousDayData(string filePath)
        {
            try
            {
                if (!string.IsNullOrEmpty(filePath) && filePath.LastIndexOf("_") > 0)
                {
                    string currentDay = filePath.Substring(filePath.LastIndexOf("_") + 1, 8);
                    string folderPath = Utility.GetAppSetting("TempDirectory_Download");
                    if (Directory.Exists(folderPath))
                    {
                        foreach (string entry in Directory.GetFileSystemEntries(folderPath))
                        {
                            if (File.Exists(entry) && entry.LastIndexOf("_") > 0)
                            {
                                if (entry.Substring(entry.LastIndexOf("_") + 1).Length == 23)//yyyyMMddHHmmssffff + .docm
                                {
                                    string generateDate = entry.Substring(entry.LastIndexOf("_") + 1, 8);
                                    if (generateDate != currentDay)
                                    {
                                        File.Delete(entry);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {

            }
        }
/// <summary>
        /// Copy file to temp path
        /// </summary>
        /// <param name="path1">file full path</param>
        /// <param name="path2">the temp full path to be copied to</param>
        /// <returns></returns>
        private string CopyFileToTempServer(string path1, string path2)
        {
            string errMsg = "";
            try
            {
                FileInfo fi = new FileInfo(path1);
                //delete file which generated at previous day in the temp file
                DeletePreviousDayData(path2);
                FileInfo fi1 = new FileInfo(path2);
                if (fi1.Exists)
                {
                    fi1.Delete();
                }
                //copy to the temp folder
                if (fi.Exists)
                {
                    fi.CopyTo(path2);
                }



            }
            catch
            {
                errMsg = "Copy file to " + path2 + " failed. Maybe you don't have its permission, or the temp file couldnot be update f                    or its readonly, please check it!";// += ex.ToString();
            }
            return errMsg;
        }

 

转载于:https://www.cnblogs.com/liuwj/p/4860840.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值