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();
           }

  • 相关阅读:
    火狐URL编码问题
    Asp.net动态关键字
    charindex ,PATINDEX,contains,FREETEXT用法
    PanGu词库批量添加关键词
    Dictionary SortedList HashSet List用法
    hubbledotnet查询速度慢的问题
    asp.net 页面static变量问题
    String.Concat和String.Format用法
    .net显示今天农历的代码
    存储过程用户登录
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050648.html
Copyright © 2011-2022 走看看