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

      string s1 = string.Empty;
                //读取文件
                try
                {

                    FileStream fs = new FileStream("F:\\test.txt", FileMode.Open);
                    StreamReader sr = new StreamReader(fs);
                    while (sr.ReadLine() != null)
                    {
                        s1 += sr.ReadLine();
                    }
                    //string s2 = sr.ReadToEnd();
                    sr.Close();
                }
                catch (Exception err)
                {

                    throw new Exception("异常信息为:" + err.Message);
                }
                finally
                {

                }
                string str1= "";
                string str;
                try
                {
                    FileStream file = new FileStream("F:\\test.txt", FileMode.Open);
                    StreamReader sr = new StreamReader(file);
                    while ((str=sr.ReadLine()) != null)
                    {
                        str1 += str;
                    }
                    //或者str = sr.ReadToEnd();
                    sr.Close();
                }
                catch
                { }

                string[] s = File.ReadAllLines("F:\\test.txt");
                //string s = textBox3.Text;
                //string[] lines = s.Split(new char[] {'\r','\n' }, StringSplitOptions.RemoveEmptyEntries);
                string[] lines = textBox3.Lines;
                int maxScore = 0;
                string Name = string.Empty;
                foreach (var line in lines)
                {
                    string[] strs = line.Split('=');
                    int score = int.Parse(strs[1]);
                    if (score > maxScore)
                    {
                        maxScore = score;
                        Name = strs[0];
                    }
                }
                label1.Text = maxScore.ToString();

           /// <summary>
           /// 写日志,方便测试,看网站需求(也可以将其写到数据库)
           /// </summary>
           /// <param name="sPath"></param>
           /// <param name="sWord"></param>
           public static void log_result(string sPath,string sWord)
           {
               StreamWriter fs = new StreamWriter(sPath, false, System.Text.Encoding.Default);
               fs.Write(sWord);
               fs.Close();
           }

  • 相关阅读:
    工作之余
    用MFC如何高效地绘图
    C++运算符优先级
    CentOS5.9下用Kate
    3G门户网(3G.cn) 招聘 软件测试工程师
    深圳市东润信息咨询有限公司招聘职位: 3G无线产品经理
    广州杰赛科技股份有限公司 招聘 技术中心3G协议软件工程师
    3G门户网(3G.cn) 招聘 手机游戏开发工程师
    3G门户网(3G.cn) 招聘 技术支持工程师
    3G工程师:三大热门的3G职业资格培训认证
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050648.html
Copyright © 2011-2022 走看看