一个常用处理文件与文件夹方法的综合类

 /// <summary>
    /// 本类说明:用来处理fso的操作!
    /// </summary>
    public partial class ReadTempleContent
    {
        #region 用来读取指定的文件
        /// <summary>
        /// 本函数用来读取文件的内容
        /// </summary>
        /// <param name="StrPath">文件的路径,相对于服务器的路径</param>
        /// <returns></returns>
        public static string ReadContent(string StrPath)
        {
            try
            {
                string StrServerPath = HttpContext.Current.Server.MapPath(StrPath);
                if (File.Exists(StrServerPath))
                {
                    return File.ReadAllText(StrServerPath, Encoding.GetEncoding("gb2312"));
                }
                else
                {
                    return "";
                }
            }
            catch
            {
                return "";
            }
        }
        #endregion

        #region 用来生成文件
        /// <summary>
        /// 用来生成新的文件
        /// </summary>
        /// <param name="StrContent">要生成的文件的内容</param>
        /// <param name="StrNewPath">生成文件保存的位置</param>
        /// <returns>true 表示生成成功,false生成失败</returns>
        public static bool CreatNew(string StrContent, string StrNewPath)
        {
            try
            {
                string StrServerPath = HttpContext.Current.Server.MapPath(StrNewPath);
                if (File.Exists(StrServerPath))
                {
                    File.Delete(StrServerPath);
                }
                FileStream FS = File.Create(StrServerPath);
                byte[] ContentByte = Encoding.GetEncoding("gb2312").GetBytes(StrContent);
                FS.Write(ContentByte, 0, ContentByte.Length);
                FS.Close();
                FS = null;
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region 向文件里追加文本
        /// <summary>
        /// 向文件里追加文本
        /// </summary>
        /// <param name="StrAddContent">要追加的文本</param>
        /// <param name="StrPath">文件的路径</param>
        /// <returns></returns>
        public static bool AddText(string StrAddContent, string StrPath)
        {
            try
            {
                string StrServerPath = HttpContext.Current.Server.MapPath(StrPath);
                File.AppendAllText(StrServerPath, StrAddContent);
                return true;
            }
            catch
            {
                return false;
            }

        }
        #endregion

        #region 删除指定的文件
        /// <summary>
        /// 删除指定路径的文件
        /// </summary>
        /// <param name="StrPath">指定文件的路径</param>
        /// <returns></returns>
        public static bool DeleteFile(string StrPath)
        {
            try
            {
                string StrServerPath = HttpContext.Current.Server.MapPath(StrPath);
                if (File.Exists(StrServerPath))
                {
                    File.Delete(StrServerPath);
                }
                return true;
            }
            catch
            {
                return false;//删除数据失败!
            }
        }
        #endregion

        #region 用来创建文件夹
        /// <summary>
        /// 用来创建文件夹
        /// </summary>
        /// <param name="FolderPath">文件路径</param>
        /// <returns></returns>
        public static string CreatFolder(string FolderPath,string cid)
        {
            try
            {
                string StrFolderServerPath = HttpContext.Current.Server.MapPath(FolderPath);
                if (!Directory.Exists(StrFolderServerPath))
                {
                    Directory.CreateDirectory(StrFolderServerPath);                   
                }
                string StrFolderServerPathc = HttpContext.Current.Server.MapPath( FolderPath+"/"+cid );
                if ( !Directory.Exists( StrFolderServerPathc ) )
                {
                    Directory.CreateDirectory( StrFolderServerPathc );
                }
                return FolderPath+"/"+cid+"/";
            }
            catch
            {
                return "";
            }
        }
        #endregion

        #region 用来删除整个文件夹,及其文件夹的内容
        /// <summary>
        /// 用来删除文件夹
        /// </summary>
        /// <param name="FolderName">要删除的文件夹的名字</param>
        /// <returns></returns>
        public static bool DeleteFolder(string FolderName)
        {
            if (FolderName.Trim().Length > 0)
            {
                try
                {
                    string FolderPath = System.Web.HttpContext.Current.Server.MapPath(FolderName);
                    if (System.IO.Directory.Exists(FolderPath))
                    {
                        System.IO.Directory.Delete(FolderPath, true);
                        return true;
                    }
                    else
                    {
                        return true;
                    }
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 删除文件
        /// <summary>
        /// 删除父文件夹
        /// </summary>
        /// <param name="FolderPathName">要删除的文件夹的路径名</param>
        /// <returns></returns>
        public static bool DeleParentFolder(string FolderPathName)
        {
            try
            {
                string strPath = HttpContext.Current.Server.MapPath(FolderPathName);
                DirectoryInfo DelFolder = new DirectoryInfo(strPath);
                if (DelFolder.Exists)
                {
                    DelFolder.Delete();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region 用来得到一个目录下的所有目录的
        public static DataTable GetDir(string FilePath)
        {
            DataTable tb = new DataTable();
            tb.Columns.Add("name", typeof(string));
            tb.Columns.Add("fullname", typeof(string));
            string strPath = HttpContext.Current.Server.MapPath(FilePath);
            DirectoryInfo dir = new DirectoryInfo(strPath);

            foreach (DirectoryInfo dChild in dir.GetDirectories("*"))
            {
                //如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
                DataRow Dr = tb.NewRow();
                Dr["name"] = dChild.Name; //打印目录名
                Dr["fullname"] = FilePath+"/"+dChild.Name; //打印路径和目录名
                tb.Rows.Add(Dr);
            }
            return tb;
        }
        #endregion

        #region 用来遍历一个目录下的所有文件并找到对应文件后删除!
        public static void  GetDeleteFile(string FilePath,string filename)
        {
            string strPath = HttpContext.Current.Server.MapPath(FilePath);
            DirectoryInfo dir = new DirectoryInfo(strPath);
            foreach (DirectoryInfo dChild in dir.GetDirectories("*"))
            {
                GetDeleteFile(FilePath + "/" + dChild.Name, filename);
                if (dChild.Name == filename)
                {
                    DeleteFolder(FilePath + "/" + dChild.ToString());
                }
            }
        }
        #endregion
    }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值