c#读取文件

来源:互联网 发布:大华网络视频解码器 编辑:程序博客网 时间:2024/05/22 04:55

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

原创粉丝点击