zoukankan      html  css  js  c++  java
  • C# 读写txt文件方法

    添加引用:

    using System.IO;

    1.File类写入文本文件:

    private void btnTextWrite_Click(object sender, EventArgs e)
            {
                //文件路径
                string filePath = @"E:123456.txt";
    
                //检测文件夹是否存在,不存在则创建
                NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));
    
                //定义编码方式,text1.Text为文本框控件中的内容
                byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
                string mystr1 = Encoding.UTF8.GetString(mybyte);
    
                //写入文件
                //File.WriteAllBytes(filePath,mybyte);//写入新文件
                //File.WriteAllText(filePath, mystr1);//写入新文件
                File.AppendAllText(filePath, mystr1);//添加至文件
    
            }

    2.File类读取文本文件:

    private void btnTexRead_Click(object sender, EventArgs e)
            {
                //文件路径
                string filePath = @"E:123456.txt";
                try
                {
                    if (File.Exists(filePath))
                    {
                        text1.Text = File.ReadAllText(filePath);
                        byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
                        text1.Text = Encoding.UTF8.GetString(mybyte);
                    }
                    else
                    {
                        MessageBox.Show("文件不存在");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    3.StreamWrite类写入文本文件:

    private void btnTextWrite_Click(object sender, EventArgs e)
            {
                //文件路径
                string filePath = @"E:123456.txt";
    
                try
                {
                    //检测文件夹是否存在,不存在则创建
                    string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));
    
                    using (StreamWriter sw = new StreamWriter(filePath, false, Encoding.UTF8))
                    {
                        byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
                        text1.Text = Encoding.UTF8.GetString(mybyte);
                        sw.Write(text1.Text);
                    }
    
                }
                catch
                {
    
                }
            }

    4.StreamReader类读取文本文档:

    private void btnTexRead_Click(object sender, EventArgs e)
            {
                //文件路径
                string filePath = @"E:123456.txt";
                try
                {
                    if (File.Exists(filePath))
                    {
                        using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8))
                        {
                            text1.Text = sr.ReadToEnd();
                            byte[] mybyte = Encoding.UTF8.GetBytes(text1.Text);
                            text1.Text = Encoding.UTF8.GetString(mybyte);
                        }
                    }
                    else
                    {
                        MessageBox.Show("文件不存在");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    ----------------------------------------------------------------------------------------------

    遇到的常见错误:

    1.ERROR: “System.Web.Mvc.Controller.File(string, string, string)”是一个“方法”,这在给定的上下文中无效

    解决方法:Controller.File方法和System.IO.File类名称冲突的问题,只要完整输入明确类名就可解决。

    例如:File.ReadAllText();  改为  System.IO.File.ReadAllText();

  • 相关阅读:
    查看数据表的信息
    HR面试必问:你什么时候能到岗?聪明人都这样回答
    42种常见的HTTP响应代码
    Ubuntu环境下微信界面中间显示黑框和字体显示为白框等问题
    C# 开发电子印章制作工具 -- 附下载程序
    ofd文件拆分合并思路探索 -- 附下载程序
    利用防火墙出站规则解决Adobe Rader XI闪退问题
    记一次 Aximp.exe工具的使用
    MacOS Monterey 一键重置系统
    macbook m1 键盘进灰导致个别按键不灵敏解决方法
  • 原文地址:https://www.cnblogs.com/stilldream/p/10044011.html
Copyright © 2011-2022 走看看