c# 文件操作类

    
    /// <summary>
    /// 文件操作类
    /// </summary>
    class FileHelper
    {
        /// <summary>
        /// 要读取的文件完全限定名
        /// </summary>
        /// <param name="path"></param>
        public FileHelper(string path)
        {
            this.FilePath = path;
        }

        /// <summary>
        /// 属性 文件路径
        /// </summary>
        string filePath;
        public string FilePath
        {
            get { return filePath; }
            set { filePath = value; }
        }

        /// <summary>
        /// 得到文件的完全名字
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string FullFileName(string path)
        {
            return Path.GetFileName(path);//path不存在就返回path
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="context"></param>
        public void WriteFile(string context/*写入内容*/)
        {
            if (!File.Exists(this.FilePath)) /*判断文件是否存在,存在true不存在false*/
            {
                using (StreamWriter sw = File.CreateText(this.FilePath))
                {
                    TextWriter tw = TextWriter.Synchronized(sw); //不明白这两个的区别,但是效果是一样地,看到网上有人这样写,因为安全?!谁了解讲讲啊
                    tw.Write(context);
                    tw.Close();
                    //sw.Write(context);
                }
            }
        }

        /// <summary>
        /// 读文件
        /// </summary>
        /// <returns></returns>
        public string ReadFile()
        {
            using (StreamReader sr = File.OpenText(this.FilePath))
            {
                //return sr.ReadToEnd();     //方案一从头一口气读到尾

                StringBuilder sb = new StringBuilder(100); //方案二一小口气读一行,劳逸结合
                string str = string.Empty;
                if ((str = sr.ReadLine()) != null)
                {
                    sb.Append(str);
                    sb.Append(@"\r\n");
                }
                return sb.ToString();
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <returns></returns>
        public bool DeleteFile()
        {
            try
            {
                if (File.Exists(this.FilePath))
                {
                    File.Delete(this.FilePath);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 复制文件,将我们的文件复制到一个新文件,新文件如果已经存在那么就挂了
        /// </summary>
        /// <param name="path"></param>
        public bool CopyFile(string path)
        {
            try
            {
                if (!File.Exists(path))
                {
                    File.Copy(this.FilePath/*源文件*/ , path/*目标文件*/);
                    return true;//复制成功了
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }

转载于:https://www.cnblogs.com/raylovelc/archive/2008/03/21/1116821.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值