c#文件操作

demo1:

        /// <summary>
        /// 将字符串写入到txt文件
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="contentStr"></param>
        public void WriteTxt(string filepath,string contentStr) {
            if (!File.Exists(filepath))
            {
                logger.Info("文件不存在");
                File.CreateText(filepath);
            }

            using (StreamWriter sw = new StreamWriter(filepath))
            {
                sw.WriteLine(contentStr);
            }
        }


        public string ReadTxt(string filepath)
        {
            StringBuilder sb = new StringBuilder(1024);
            string line;
            using (StreamReader sr = new StreamReader(filepath))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    sb.AppendLine(line);
                }
            }
            return sb.ToString();
        }

posted @ 2013-07-19 10:36  沧海一滴  阅读(224)  评论(0编辑  收藏  举报