C# TXT文件的读取和写入

    /// <summary>
        /// 文件的写入
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="name">文件名称</param>
        /// <param name="contents">写入文件的内容</param>
        /// <returns></returns>
        public static KeyValuePair<bool, string> CreateFile(string path, string name, string contents)
        {
            name = path + "\\" + name;
            var pair = new KeyValuePair<bool, string>(true, name);

            //验证路径是否存在
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            try
            {
                FileStream fs;
                //验证文件是否存在,有先清空文本内容,然后追加,否则则创建
                if (File.Exists(name))
                {
                    ClearTxt(name);
                    fs = new FileStream(name, FileMode.Append, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(name, FileMode.Create, FileAccess.Write);
                }

                //这里注意是默认编码 ANSI,不需要utf-8 
                using (var sw = new StreamWriter(fs, Encoding.Default))
                {
                    sw.Write(contents);
                    sw.Close();
                }
                fs.Close();
            }
            catch (Exception ex)
            {
                pair = new KeyValuePair<bool, string>(false, ex.Message);
            }
            return pair;
        }

        /// <summary>
        /// 清空文本内容
        /// </summary>
        /// <param name="txtPath"></param>
        public static void ClearTxt(string txtPath)
        {
            using (var stream = File.Open(txtPath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.SetLength(0);

                stream.Close();
            }
        }
        /// <summary>
        /// 文件的读取
        /// </summary>
        /// <param name="path">文件指定的路径</param>
        /// <returns></returns>
        public static KeyValuePair<bool, List<string>> ReadFile(string path)
        {
            var pair = new KeyValuePair<bool, List<string>>();
            var str = new List<string>();
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                    {
                        while (!sr.EndOfStream)
                        {
                            string sLine = sr.ReadLine();
                            if (sLine.Length < 1)
                            {
                                continue;
                            }
                            str.Add(sLine);
                        }
                    }
                }
                if (str.Contains(""))
                {
                    str.Add("读取文件的内容为空");
                    pair = new KeyValuePair<bool, List<string>>(false, str);
                }
                else
                {
                    pair = new KeyValuePair<bool, List<string>>(true, str);
                }
            }
            catch (Exception ex)
            {
                str.Add(ex.Message);
                pair = new KeyValuePair<bool, List<string>>(false, str);
            }
            return pair;
        }

调用示例:

读取:basCommon.ReadFile("http://*****路径/info.txt")

写入:basCommon.CreateFile("http://*****路径", "文件名称.txt", 文件内容);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值