写入到txt的类

来源:互联网 发布:临汾行知学校简介 编辑:程序博客网 时间:2024/05/23 15:35
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;namespace AutoService{    class WriteLog    {        static String path = AppDomain.CurrentDomain.BaseDirectory;//当前服务所在路径        static String fileName = path+"ServiceLog.txt";        static StreamWriter sw = null;        static StreamReader sr = null;        static char[] ch;        private static bool isUse = true;        private static void init()        {            string a = Environment.CurrentDirectory;            if (File.Exists(fileName))            {                sr = new StreamReader(fileName);                int length = (int)sr.BaseStream.Length;                ch = new char[length];                sr.ReadBlock(ch, 0, length);                sr.Close();                sw = new StreamWriter(fileName);                sw.Write(new String(ch));            }            else            {                sw = new StreamWriter(fileName);            }        }        public static void toLog(String log)        {            if (isUse)            {                init();                isUse = false;            }            sw.WriteLine(DateTime.Now.ToString() + " " + log);            sw.Flush();        }    }}


 

原创粉丝点击