1、关于读写文件,犯的一个低级错误,平常代码拷贝习惯了,就像电脑用多了会提笔忘字一样,所以平常还是要多多用心才好。

    这段代码的意图是在文件中写入数据,如果原文件不存在,则先新建。

    事实上,当真的执行了System.IO.File.Create(filename); 再执行System.IO.StreamWriter sr=new System.IO.StreamWriter(filename,true)时会报错:

    The process cannot access the file 'C:\Documents\bigtext\1.txt' because it is being used by another process.

   原因:System.IO.File.Create(filename) 返回值是FileStream,执行了新建但是并没有关闭FileStream,所以文件used by another process.

        private static void write()
        {
            try
            {
                string filename = @"C:\Documents\bigtext\1.txt";
                if (!System.IO.File.Exists(filename))
                {
                    System.IO.File.Create(filename);
                }
                using (System.IO.StreamWriter sr=new System.IO.StreamWriter(filename,true))
                {
                    sr.WriteLine("test12");
                }
            }
            catch
            {

            }
        }

修改如下即可

        private static void write()
        {
            try
            {
                string filename = @"C:\Users\xiaochun-zhai\Documents\bigtext\1.txt";
                if (!System.IO.File.Exists(filename))
                {
                   System.IO.FileStream sr= System.IO.File.Create(filename);
                   sr.Close();
                }
                using (System.IO.StreamWriter sr=new System.IO.StreamWriter(filename,true))
                {
                    sr.WriteLine("test12");
                }
            }
            catch
            {

            }
        }

2、事实上一句代码就可代替上面的方法,以上方法仅仅为了说明System.IO.File.Create使用时要注意的问题。

string filename = @"C:\Documents\bigtext\1.txt";
System.IO.File.AppendAllText(filename, "ceshi\r\n");

 3、一个极简单的写日志的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace writefile
{
    public class WriteLog
    {
        public static string LogBasePath = @"C:\";
        private string logFileName = "Log.txt";
       
        private static object _locker = new object();
        private static WriteLog _instance;
        private WriteLog()
        {
        }

        public static WriteLog Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new WriteLog();
                        }
                    }
                }
                return _instance;
            }
        }
        public void LogMessage(string message)
        {
            lock (_locker)
            {
                string fullPath = System.IO.Path.Combine(WriteLog.LogBasePath, logFileName);
                System.IO.File.AppendAllText(fullPath, message + Environment.NewLine);
            }
        } 
    }
}

调用: WriteLog.Instance.LogMessage("test");

说明:Environment.NewLine 代表换行

posted on 2015-12-30 12:24  AmyAlisa  阅读(809)  评论(0编辑  收藏  举报