IO读、写文件异常

浪子er 2014-08-06 08:03:12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

namespace TEST
{
class Program
{
static string pathStreamWrite;
static string pathFileStream;
static void Main(string[] args)
{
pathStreamWrite = @"E:\a.txt";
pathFileStream = @"E:\b.txt";
/*如果文件不存在,在实例化StreamWrite时报错
*【错误提示:“System.IO.IOException”类型的未经处理的异常
*在 mscorlib.dll 中发生
*其他信息: 文件“E:\a.txt”正由另一进程使用,因此该进程无法访问此文件。】
*/
Run();

Run1();//正常访问

}
static void Run()
{
if (!File.Exists(pathStreamWrite))
{
File.Create(pathStreamWrite);
}
using (StreamWriter sw = new StreamWriter(pathStreamWrite, true))
{
sw.WriteLine("测试数据");
}
}
static void Run1()
{
byte[] byteTest = Encoding.UTF8.GetBytes("测试数据");
using (FileStream file = new FileStream(pathFileStream, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
file.Write(byteTest, 0, byteTest.Length);
}
}
}
}
...全文
150 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
於黾 2014-08-07
  • 打赏
  • 举报
回复
但是如此一来,既然已经定义了个filestream,直接filestream.write就好了 再定义一个streamwriter干嘛
F15Eagle 2014-08-07
  • 打赏
  • 举报
回复
在理,必须close一下。
於黾 2014-08-07
  • 打赏
  • 举报
回复
改为 FileStream FS=File.Create("1.txt"); FS.Close(); 再执行后面的写入,就好了.
梦吟情诗 2014-08-07
  • 打赏
  • 举报
回复
你先看下File.Create返回的是啥子?是FileStream,是流,说明本来就会占用文件; 你可以参考下面代码

static void Run()
        {
            if (!File.Exists(pathStreamWrite))
            {
                StreamWriter sw = File.CreateText(pathStreamWrite);
                sw.WriteLine("测试数据");
                sw.Close();
            }
            else
            {
                using (StreamWriter sw = new StreamWriter(pathStreamWrite, true))
                {
                    sw.WriteLine("测试数据");
                }
            }
        }
於黾 2014-08-07
  • 打赏
  • 举报
回复
使用file.create,文件会一直被程序占用,当然就无法操作了.
於黾 2014-08-07
  • 打赏
  • 举报
回复
RUN1不是正常么,为什么又弄了个RUN 这都什么写法啊
浪子er 2014-08-07
  • 打赏
  • 举报
回复
引用 4 楼 allstudycsharp 的回复:
你先看下File.Create返回的是啥子?是FileStream,是流,说明本来就会占用文件; 你可以参考下面代码

static void Run()
        {
            if (!File.Exists(pathStreamWrite))
            {
                StreamWriter sw = File.CreateText(pathStreamWrite);
                sw.WriteLine("测试数据");
                sw.Close();
            }
            else
            {
                using (StreamWriter sw = new StreamWriter(pathStreamWrite, true))
                {
                    sw.WriteLine("测试数据");
                }
            }
        }
没注意看 File.Craete()方法的返回值,受教了!!
浪子er 2014-08-06
  • 打赏
  • 举报
回复
刚刚执行了下 直接调用Run方法还是异常!!!

            if (!File.Exists(pathStreamWrite))
            {
                File.Create(pathStreamWrite);
            }
            using (StreamWriter sw = new StreamWriter(pathStreamWrite, true))
            {
                sw.WriteLine("测试数据");
            }
这里有问题!!!

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧