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

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

ASP.NET公共類庫之文件實(shí)用類FileUtil.cs

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

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

namespace Svnhost.Common
{
    /// <summary>
    /// 文件實(shí)用類
    /// </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">目標(biāo)文件名</param>
        /// <param name="overwrite">當(dāng)目標(biāo)文件存在時是否覆蓋</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>
        /// 備份文件,當(dāng)目標(biāo)文件存在時覆蓋
        /// </summary>
        /// <param name="sourceFileName">源文件名</param>
        /// <param name="destFileName">目標(biāo)文件名</param>
        /// <returns>操作是否成功</returns>
        public static bool BackupFile(string sourceFileName, string destFileName)
        {
            return BackupFile(sourceFileName, destFileName, true);
        }


        /// <summary>
        /// 恢復(fù)文件
        /// </summary>
        /// <param name="backupFileName">備份文件名</param>
        /// <param name="targetFileName">要恢復(fù)的文件名</param>
        /// <param name="backupTargetFileName">要恢復(fù)文件再次備份的名稱,如果為null,則不再備份恢復(fù)文件</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);
        }
    }
}
主站蜘蛛池模板: 桃源县| 简阳市| 时尚| 昌平区| 炉霍县| 鄄城县| 忻城县| 城口县| 水城县| 辽宁省| 张家港市| 汝州市| 靖安县| 昆明市| 静海县| 东台市| 海林市| 翁牛特旗| 台江县| 岑巩县| 方正县| 龙川县| 平邑县| 靖西县| 吴川市| 马鞍山市| 东兴市| 察隅县| 阿巴嘎旗| 凉城县| 鄂尔多斯市| 邢台市| 富民县| 吴江市| 金堂县| 郯城县| 鄂州市| 蓝田县| 稷山县| 遂宁市| 常山县|