zoukankan      html  css  js  c++  java
  • C#读取文件

    private void button2_Click(object sender, EventArgs e)
    {
        string file = "";
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.InitialDirectory = "c:\";//注意这里写路径时要用c:\而不是c:
        openFileDialog.Filter = "Html|*.html|Mht|*.mht|htm|*.htm";
        openFileDialog.RestoreDirectory = true;
        openFileDialog.FilterIndex = 1;
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            file = openFileDialog.FileName;


            //string fileContent = File.ReadAllText(file, Encoding.Default);//第一种方式

            //第二种方式
            string strLine = "";
            using (System.IO.StreamReader sr = new System.IO.StreamReader(file,Encoding.Default))
            {                   
                strLine = sr.ReadToEnd();
            }
            //System.Console.WriteLine(strLine);
            this.textBox1.Text = strLine;
        }

    }

    读出来的有可能出出现乱码,这个主要是跟编码格式有关,可以尝试去设置Encoding的属性去修改编码格式。

  • 相关阅读:
    bash 学习笔记5
    zookeeper启动脚本修改
    java enmu 使用说明
    bash 学习笔记4
    zookeep集群搭建说明
    bash 学习笔记3
    集群配置问题
    hadoop hdfs 总结(1)
    bash 学习笔记6
    bash pitfall
  • 原文地址:https://www.cnblogs.com/pnljs/p/3160634.html
Copyright © 2011-2022 走看看