欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號)    在線QQ:181796286
NEWS BLOG ·
學無止境
關(guān)注開優(yōu)網(wǎng)絡(luò) 關(guān)注前沿
ASP.NET公共類庫之日期格式處理類DateUtil.cs
ASP.NET公共類庫之上傳類HtmlInputFileControl.cs

ASP.NET公共類庫之文件實用類FileUtil.cs

發(fā)表日期:2015-09-05    文章編輯:南昌開優(yōu)網(wǎng)絡(luò)    瀏覽次數(shù):3857    標簽:ASP.NET應用

/**********************************************
 * 類作用:   文件實用類
* 建立人:   開優(yōu)網(wǎng)絡(luò)
 ***********************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;

namespace Svnhost.Common
{
    /// <summary>
    /// 文件實用類
    /// </summary>
    public sealed class FileUtil
    {
        private FileUtil()
        {
        }

        /// <summary>
        /// 創(chuàng)建目錄
        /// </summary>
        /// <param name="FileOrPath">文件或目錄</param>
        public static void CreateDirectory(string FileOrPath)
        {
            if (FileOrPath != null)
            {
                string path;
                if (FileOrPath.Contains("."))
                    path = Path.GetDirectoryName(FileOrPath);
                else
                    path = FileOrPath;

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
        }

        /// <summary>
        /// 讀取文件
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="encoding"></param>
        /// <param name="isCache"></param>
        /// <returns></returns>
        public static string ReadFile(string filename, Encoding encoding, bool isCache)
        {
            string body;
            if (isCache)
            {
                body = (string)HttpContext.Current.Cache[filename];
                if (body == null)
                {
                    body = ReadFile(filename, encoding, false);
                    HttpContext.Current.Cache.Add(filename, body, new System.Web.Caching.CacheDependency(filename), DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
                }
            }
            else
            {
                using (StreamReader sr = new StreamReader(filename, encoding))
                {
                    body = sr.ReadToEnd();
                }
            }

            return body;
        }

        /// <summary>
        /// 備份文件
        /// </summary>
        /// <param name="sourceFileName">源文件名</param>
        /// <param name="destFileName">目標文件名</param>
        /// <param name="overwrite">當目標文件存在時是否覆蓋</param>
        /// <returns>操作是否成功</returns>
        public static bool BackupFile(string sourceFileName, string destFileName, bool overwrite)
        {
            if (!System.IO.File.Exists(sourceFileName))
            {
                throw new FileNotFoundException(sourceFileName + "文件不存在!");
            }
            if (!overwrite && System.IO.File.Exists(destFileName))
            {
                return false;
            }
            try
            {
                System.IO.File.Copy(sourceFileName, destFileName, true);
                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        /// <summary>
        /// 備份文件,當目標文件存在時覆蓋
        /// </summary>
        /// <param name="sourceFileName">源文件名</param>
        /// <param name="destFileName">目標文件名</param>
        /// <returns>操作是否成功</returns>
        public static bool BackupFile(string sourceFileName, string destFileName)
        {
            return BackupFile(sourceFileName, destFileName, true);
        }


        /// <summary>
        /// 恢復文件
        /// </summary>
        /// <param name="backupFileName">備份文件名</param>
        /// <param name="targetFileName">要恢復的文件名</param>
        /// <param name="backupTargetFileName">要恢復文件再次備份的名稱,如果為null,則不再備份恢復文件</param>
        /// <returns>操作是否成功</returns>
        public static bool RestoreFile(string backupFileName, string targetFileName, string backupTargetFileName)
        {
            try
            {
                if (!System.IO.File.Exists(backupFileName))
                {
                    throw new FileNotFoundException(backupFileName + "文件不存在!");
                }
                if (backupTargetFileName != null)
                {
                    if (!System.IO.File.Exists(targetFileName))
                    {
                        throw new FileNotFoundException(targetFileName + "文件不存在!無法備份此文件!");
                    }
                    else
                    {
                        System.IO.File.Copy(targetFileName, backupTargetFileName, true);
                    }
                }
                System.IO.File.Delete(targetFileName);
                System.IO.File.Copy(backupFileName, targetFileName);
            }
            catch (Exception e)
            {
                throw e;
            }
            return true;
        }

        public static bool RestoreFile(string backupFileName, string targetFileName)
        {
            return RestoreFile(backupFileName, targetFileName, null);
        }
    }
}
主站蜘蛛池模板: 鹿邑县| 封开县| 大名县| 盘山县| 论坛| 石河子市| 姜堰市| 昆山市| 沙河市| 闽清县| 荥阳市| 长阳| 开鲁县| 衡水市| 新兴县| 吴堡县| 桦南县| 盘锦市| 东台市| 孝义市| 策勒县| 美姑县| 淅川县| 通河县| 青铜峡市| 延庆县| 灌阳县| 迁西县| 堆龙德庆县| 麦盖提县| 湖南省| 河源市| 栾城县| 弋阳县| 元朗区| 泊头市| 长宁区| 胶州市| 盈江县| 班玛县| 内乡县|