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的属性去修改编码格式。

  • 相关阅读:
    Linux 搭建svn环境
    Echarts 获取后台数据 使用后台数据展示 饼装图
    js 实现存储Map 结构的数据
    SVN使用方法
    SVN版本回退
    adf 日志输出
    Oracle ADF VO排序及VO的查询模式
    weblogic 初始化
    jdeveloper 恢复默认配置
    jdeveloper12.1.3的安装与卸载
  • 原文地址:https://www.cnblogs.com/pnljs/p/3160634.html
Copyright © 2011-2022 走看看