国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Asp.net對文件夾和文件的操作類

2019-11-18 17:15:24
字體:
來源:轉載
供稿:網友

using System;
using System.IO;
using System.Web;

namespace SEC
{
    /**//// <summary>
    /// 對文件和文件夾的操作類
    /// </summary>
    public class FileControl
    {
        public FileControl()
        {
           
        }
        /**//// <summary>
        /// 在根目錄下創建文件夾
        /// </summary>
        /// <param name="FolderPath">要創建的文件路徑</param>
        public void CreateFolder(string FolderPathName)
        {
            if(FolderPathName.Trim().Length>0)
            {
                try
                {
                    string CreatePath = System.Web.HttpContext.Current.Server.MapPath

("../../../Images/"+FolderPathName).ToString();
                    if(!Directory.Exists(CreatePath))
                    {
                        Directory.CreateDirectory(CreatePath);
                    }     
                }
                catch
                {
                    throw;
                }
            }
        }

        /**//// <summary>
        /// 刪除一個文件夾下面的字文件夾和文件
        /// </summary>
        /// <param name="FolderPathName"></param>
        public void DeleteChildFolder(string FolderPathName)
        {
            if(FolderPathName.Trim().Length>0)
            {
                try
                {
                    string CreatePath = System.Web.HttpContext.Current.Server.MapPath

(FolderPathName).ToString();
                    if(Directory.Exists(CreatePath))
                    {
                        Directory.Delete(CreatePath,true);
                    }     
                }
                catch
                {
                    throw;
                }
            }
        }

        /**//// <summary>
        /// 刪除一個文件
        /// </summary>
        /// <param name="FilePathName"></param>
        public void DeleteFile(string FilePathName)
        {
            try
            {
                FileInfo DeleFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath

(FilePathName).ToString());
                DeleFile.Delete();
            }
            catch
            {
            }
        }
        public void CreateFile(string FilePathName)
        {
            try
            {
                //創建文件夾
                string[] strPath= FilePathName.Split('/');
                CreateFolder(FilePathName.Replace("/" + strPath[strPath.Length-1].ToString(),"")); //創建文件


                FileInfo CreateFile =new FileInfo(System.Web.HttpContext.Current.Server.MapPath

(FilePathName).ToString());         //創建文件
                if(!CreateFile.Exists)
                {
                    FileStream FS=CreateFile.Create();     
                    FS.Close();
                }
            }
            catch
            {
            }
        }
        /**//// <summary>
        /// 刪除整個文件夾及其字文件夾和文件
        /// </summary>
        /// <param name="FolderPathName"></param>
        public void DeleParentFolder(string FolderPathName)
        {
            try
            {
                DirectoryInfo DelFolder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath

(FolderPathName).ToString());
                if(DelFolder.Exists)
                {
                    DelFolder.Delete(); 
                }
            }
            catch
            {
            }
        }
        /**//// <summary>
        /// 在文件里追加內容
        /// </summary>
        /// <param name="FilePathName"></param>
        public void ReWriteReadinnerText(string FilePathName,string WriteWord)
        {
            try
            {
                //建立文件夾和文件
                //CreateFolder(FilePathName);
                CreateFile(FilePathName);
                //得到原來文件的內容
                FileStream FileRead=new FileStream(System.Web.HttpContext.Current.Server.MapPath

(FilePathName).ToString(),FileMode.Open,Fileaccess.ReadWrite);
                StreamReader FileReadWord=new StreamReader(FileRead,System.Text.Encoding.Default);
                string OldString = FileReadWord.ReadToEnd().ToString();
                OldString = OldString + WriteWord; 
                //把新的內容重新寫入
                StreamWriter FileWrite=new StreamWriter(FileRead,System.Text.Encoding.Default);
                FileWrite.Write(WriteWord);
                //關閉
                FileWrite.Close();
                FileReadWord.Close();
                FileRead.Close();
            }
            catch
            {
                //    throw;
            }
        }

        /**//// <summary>
        /// 在文件里追加內容
        /// </summary>
        /// <param name="FilePathName"></param>
        public string ReaderFileData(string FilePathName)
        {
            try
            {
    
                FileStream FileRead=new FileStream(System.Web.HttpContext.Current.Server.MapPath

(FilePathName).ToString(),FileMode.Open,FileAccess.Read);
                StreamReader FileReadWord=new StreamReader(FileRead,System.Text.Encoding.Default);
                string TxtString = FileReadWord.ReadToEnd().ToString();                
                //關閉
                FileReadWord.Close();
                FileRead.Close();
                return TxtString;
            }
            catch
            {
                throw;
            }
        }
        /**//// <summary>
        /// 讀取文件夾的文件
        /// </summary>
        /// <param name="FilePathName"></param>
        /// <returns></returns>
        public DirectoryInfo checkValidsessionPath(string FilePathName)
        {               
            try
            {
                DirectoryInfo MainDir = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath

(FilePathName));
                return MainDir;    
               
            }
            catch
            {
                throw;
            }
        }
    }
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石嘴山市| 玉门市| 双辽市| 朝阳县| 中卫市| 洱源县| 新丰县| 娱乐| 灯塔市| 通州区| 黄骅市| 富蕴县| 长武县| 荔波县| 姚安县| 准格尔旗| 凉山| 德江县| 姜堰市| 凤庆县| 洮南市| 阳新县| 康定县| 墨玉县| 哈巴河县| 鄂温| 兴安盟| 曲松县| 岳池县| 江陵县| 包头市| 滦平县| 肇庆市| 襄汾县| 阿克陶县| 长葛市| 拉萨市| 当涂县| 鲁山县| 平舆县| 仪征市|