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

推荐订阅源

L
LangChain Blog
J
Java Code Geeks
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
雷峰网
雷峰网
D
DataBreaches.Net
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 聂微东
V
Visual Studio Blog
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta

博客园 - 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;
            }
        }
    }