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

  • 相关阅读:
    Makefile编写
    C++静态库与动态库
    C语言编译过程详解
    Fiddler抓HTTPS
    web测试的一些关注点
    Appium使用过程中的一些坑
    Jmeter+Maven+Jenkins的搭建笔记
    华为DHCP+VLANDHCP RELAY配置重点
    华为GVRP配置重点
    802.11协议总结
  • 原文地址:https://www.cnblogs.com/pnljs/p/3160634.html
Copyright © 2011-2022 走看看