zoukankan      html  css  js  c++  java
  • C#写入TXT文件

    using System.IO;
    using System.Diagnostics;
    namespace WebApplication1
    {
        public class FileText
        {
            public static string FileAddress = "c:\\error.txt";
            /// <summary>
            /// 记录错误的类名和方法名及错误信息
            /// </summary>
            /// <param name=”className”>出错的类名</param>
            /// <param name=”ex”>错误实例</param>
            public static void Record(Exception ex)
            {
                try
                {
                    FileStream fs = null;
                    if (!File.Exists(FileAddress))
                    {
                        fs = new FileStream(FileAddress, FileMode.Create);
                    }
                    else
                    {
                        fs = new FileStream(FileAddress, FileMode.Append);
                    }
                    StackTrace st = new StackTrace(true);
                    string transferMethod = st.GetFrame(1).GetMethod().Name.ToString();
    
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine("—————————————————————————————–");
                    sw.WriteLine("日    期:" + DateTime.Now.ToString("G"));
                    sw.WriteLine("调用错误记录的方法:" + transferMethod);
                    sw.WriteLine("引发错误的方法:" + ex.TargetSite.ToString());
                    sw.WriteLine("错误消息:" + ex.Message.ToString());
                    sw.WriteLine("—————————————————————————————–");
                    sw.Close();
                }
                catch (IOException ioe)
                {
                }
            }
    
            public static void WriteString(string Str)
            {
                FileStream fs = null;
                if (!File.Exists(FileAddress))
                {
                    fs = new FileStream(FileAddress, FileMode.Create);
                }
                else
                {
                    fs = new FileStream(FileAddress, FileMode.Append);
                }
                StackTrace st = new StackTrace(true);
                string transferMethod = st.GetFrame(1).GetMethod().Name.ToString();
    
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("—————————————————————————————–");
                sw.WriteLine("日    期:" + DateTime.Now.ToString("G"));
                sw.WriteLine("错误消息:" + Str);
                sw.WriteLine("—————————————————————————————–");
                sw.Close();
            }
        }
    }
  • 相关阅读:
    ~~网络编程(六):自定义报头~~
    ~~网络编程(五):粘包现象~~
    ~~网络编程(四):socket套接字~~
    ~~网络编程(三):TCP/UDP~~
    ~~网络编程(二):层级初识~~
    Java的异常处理方式
    Java中的泛型
    Collection接口综述
    匿名内部类和局部内部类只能访问final变量的原因
    Java内部类
  • 原文地址:https://www.cnblogs.com/wujy/p/2937845.html
Copyright © 2011-2022 走看看