• C#读取文件


        protected void Button1_Click(object sender, EventArgs e)
        {
            using (StreamWriter w = File.AppendText("log.txt"))//追边文件
            {
                Log("Test1", w);
                Log("Test2", w);
                // Close the writer and underlying file.
                w.Close();
            }
            // Open and read the file.
            using (StreamReader r = File.OpenText("log.txt"))//读取文件
            {
                DumpLog(r);
            }


        }

    public static void Log(string logMessage, TextWriter w)//
        {
            w.Write("\r\nLog Entry : ");
            w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            w.WriteLine("  :");
            w.WriteLine("  :{0}", logMessage);
            w.WriteLine("-------------------------------");
            // Update the underlying file.
            w.Flush();
        }

        public static void DumpLog(StreamReader r)
        {
            // While not at the end of the file, read and write lines.
            string line;
            while ((line = r.ReadLine()) != null)
            {
                Console.WriteLine(line);
               
            }
            r.Close();
        }

  • 相关阅读:
    20201220第二周学习总结
    师生关系
    快速浏览教材
    学期2020-2021-1学号20201220《信息安全专业导论》第1周学习总结
    编程将字符串s倒序输出,要求利用函数递归实现
    小学生四则运算随机生成程序
    礼炮问题
    C语言最大公约数
    C语言判断三角形类型
    C语言:一元二次方程解的所有情况
  • 原文地址:https://www.cnblogs.com/lucky_dai/p/2038984.html
Copyright © 2020-2023  润新知