惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
小众软件
小众软件
The Cloudflare Blog
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
美团技术团队
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
博客园 - 叶小钗
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
Y
Y Combinator Blog
D
Docker
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - pysharp

openstack horizon api step by step understanding horizon's template path what can PsTools\psexec do! - pysharp What's New in Python3.0 - pysharp [转]利用adsutil.vbs脚本创建自定义web站点 我的2008. sql基础篇,不断更新中...... TypeConvert Demo javascript 调试器v1.0.0.0 Dundas Chart Demo For New User - pysharp c#面向对象中的继承初步认识 .net 集合类初步认识 c# 目录操作类 关于urlrewrite的小DEMO - pysharp - 博客园 c# 动态编译方法 c# 读取Excel到datable asp.net 下载和在线预览Excel的方法 排序算法 c#实现 简单使用nHibernate,新手练习用。
c# 文件操作类 - pysharp - 博客园
pysharp · 2008-03-21 · via 博客园 - pysharp

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