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;
            }
        }
    }

posted @ 2008-03-21 17:34  pysharp  阅读(2188)  评论(0编辑  收藏  举报