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 代表换行

文件操作FileStream,Log的更多相关文章

  1. asp.net文件操作类

    /** 文件操作类 **/ #region 引用命名空间 using System; using System.Collections.Generic; using System.Text; usin ...

  2. C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名

    原文:C# 文件操作(全部) 追加.拷贝.删除.移动文件.创建目录 修改文件名.文件夹名 本文也收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归 ...

  3. C#对文件操作(基本的读写以及压缩和解压)

    主要是针对单个文件进行读写操作和压缩操作:用到的主要C#类有FileStream.FileInfo.StreamWrite.StreamRead.GZipStream. 字符数组和字节数组的转换: ] ...

  4. c#之文件操作(学习笔记)

    File类和Directory类 FileInfo类 需要提供一个文件路径来创建一个FileInfo类实例对象,FileInfo提供很多类似File的方法,在选择使用File还是FileInfo时应遵 ...

  5. Unity3D学习笔记(二十五):文件操作

    文件是什么? 存储在硬盘上的最后的节点. 文件夹是什么? 文件的上级单位称为文件夹. 文件夹的基本结构? 文件夹是层级化结构的,对于同级的文件夹不可以重名,父文件夹和子文件夹可以同名> IO:I ...

  6. 【基础巩固】文件流读写、大文件移动 FileStream StreamWriter File Path Directory/ ,m资料管理器(递归)

    C#获取文件名 扩展名 string fullPath = @"d:\test\default.avi"; string filename = Path.GetFileName(f ...

  7. C# 文件操作(摘抄)

    ——选自<c# 编程兵书>第11章 张志强 胡君 编著 11 文件操作概述 11.1 驱动器 在Windows操作系统中,存储介质统称为驱动器,硬盘由于可以划分为多个区域,每一个区域称为一 ...

  8. C# 文件操作【转】

    本文也收集了目前最为常用的C#经典操作文件的方法,具体内容如下:C#追加.拷贝.删除.移动文件.创建目录.递归删除文件夹及文件.指定文件夹下面的所有内容copy到目标文件夹下面.指定文件夹下面的所有内 ...

  9. C#文件操作 File(静态类)

      操作某一个文件/文件夹,需要一个文件的完整路径 一.使用File的静态方法进行文件操作 1 2 3 4 5 6 7 8 9 //使用file的静态方法进行复制             File.C ...

随机推荐

  1. 【KMP模板】POJ3461-Oulipo

    [题意] 找出第一个字符串在第二个字符串中出现次数. [注意点] 一定要先将strlen存下来,而不能每次用每次求,否则会TLE! #include<iostream> #include& ...

  2. c99柔性数组

    变长结构体 struct test { int nSize; char data[]; // 或者 char data[0];但建议使用 char data[]; 注意:c98 时不支持柔性数组,其仅 ...

  3. HDU 5636 Shortest Path 暴力

    Shortest Path 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 Description There is a path graph ...

  4. Codeforces Gym 100269D Dwarf Tower spfa

    Dwarf Tower 题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a ...

  5. 记一个有趣的Java OOM!

    原文:https://my.oschina.net/u/1462914/blog/1630086 引言 熟悉Java的童鞋,应该对OOM比较熟悉.该类问题,一般都比较棘手.因为造成此类问题的原因有很多 ...

  6. memcached与redis区别

    redis:redis是一个高级的key-value的nosql,它主要是用作存储,这是因为它具有持久化功能,并且它支持很多种的数据类型操作,例如,字符串list,set,zset,hash等数据结构 ...

  7. UITabBarControlller 和 UINavigationController

  8. gdc skin

    https://www.gdcvault.com/play/1024410/Achieving-High-Quality-Low-Cost 这篇是教美术怎么用做地形那种方法 复用贴图 做skin的 做 ...

  9. Android(Fragment和Activity之间通信)

    Fragment的使用可以让我们的应用更灵活的适配各种型号的安卓设备,但是对于Fragment和Activity之间的通信,很多朋友应该比较陌生,下面我们就通过一个实例来看一看如何实现. 一.Acti ...

  10. C++ 重写重载重定义区别

    C++ 重写重载重定义区别 (源自:http://blog.163.com/clevertanglei900@126/blog/static/111352259201102441934870/) 1 ...