C#新手文件写入问题

whb678 2012-03-06 04:40:08
string filepath_003 = @"f:\test_003.txt";
if (!File.Exists(filepath_003))
{
File.Create(filepath_003);
Console.WriteLine("创建文件" + filepath_003 + "!");
}
//StreamWriter写入数据
FileStream fs = File.Create(filepath_003, FileMode.Open, FileAccess.ReadWrite);
byte[] b = Encoding.UTF8.GetBytes("使用StreamWriter写入数据");
if (fs.CanWrite)
{
fs.Write(b, 0, b.Length);
}
fs.Close();
//StreamWriter写入数据
StreamWriter sw = new StreamWriter(filepath_003, true);
sw.Write("使用StreamWriter写入数据:");
sw.Close();


如上代码,在这句
FileStream fs = File.Create(filepath_003, FileMode.Open, FileAccess.ReadWrite);
会提示文件被占用,为什么?怎么解决?
...全文
117 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
whb678 2012-03-06
  • 打赏
  • 举报
回复
查了下Create方法:

由此方法创建的 FileStream 对象的 FileShare 值默认为 None;直到关闭原始文件句柄后,其他进程或代码才能访问这个创建的文件。

此方法等效于使用默认缓冲区大小的 Create(String, Int32) 方法重载。

允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory。

如果指定的文件不存在,则创建该文件;如果存在并且不是只读的,则将覆盖其内容。

默认情况下,将向所有用户授予对新文件的完全读/写访问权限。文件是用读/写访问权限打开的,必须关闭后才能由其他应用程序打开。
EnForGrass 2012-03-06
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yuanyuan_yangzhen 的回复:]

string filepath_003 = @"f:\test_003.txt";
if (!File.Exists(filepath_003))
{
File.Create(filepath_003);
Console.WriteLine("创建文件" + filepath_003 + "!");
}
//StreamWriter写入数据
FileSt……
[/Quote]
其实他就是少写个else
yuanyuan_yangzhen 2012-03-06
  • 打赏
  • 举报
回复
string filepath_003 = @"f:\test_003.txt";
if (!File.Exists(filepath_003))
{
File.Create(filepath_003);
Console.WriteLine("创建文件" + filepath_003 + "!");
}
//StreamWriter写入数据
FileStream fs = File.Create(filepath_003, FileMode.Open, FileAccess.ReadWrite);


楼主的意思是:文件不存在的话,创建一个文件,可是你下面又创建了一遍,所以冲突了。而且你下面写的ile.Create(filepath_003, FileMode.Open, FileAccess.ReadWrite)参数都不对,你是不是这边写错了,想写别的啊。
天二天 2012-03-06
  • 打赏
  • 举报
回复
FileInfo file = new FileInfo("f:\test_003.txt");

using (StreamWriter writer = file.AppendText())
{
message = "wgwgwg "
writer.WriteLine(message);
}
whb678 2012-03-06
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sunzongbao2007 的回复:]

引用 7 楼 porschev 的回复:
我看LZ应该是敲错了吧。。。。


你在前面己经创建过了,后面是打开


即使是open,之前的也应该close。我印象中create是文件流自动打开的状态。
[/Quote]
是啊,用open也不行,2楼帖子里说的加什么using也白搭,奇怪了......
铜臂阿铁木 2012-03-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 porschev 的回复:]
我看LZ应该是敲错了吧。。。。


你在前面己经创建过了,后面是打开
[/Quote]

即使是open,之前的也应该close。我印象中create是文件流自动打开的状态。
porschev 2012-03-06
  • 打赏
  • 举报
回复

我看LZ应该是敲错了吧。。。。


你在前面己经创建过了,后面是打开
porschev 2012-03-06
  • 打赏
  • 举报
回复



FileStream fs = File.Create(filepath_003, FileMode.Open, FileAccess.ReadWrite);

------》修改为

FileStream fs = File.Open(filepath_003, FileMode.Open, FileAccess.ReadWrite);

whb678 2012-03-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sunzongbao2007 的回复:]

引用楼主 whb678 的回复:
File.Create(filepath_003);

创建完了默认是打开的。

FileStream fileStream = File.Create(filepath_003);

接收一下,然后下面用fileStream写。

你连续创建两次filepath_003 肯定冲突了。
[/Quote]
接受一下什么意思?
铜臂阿铁木 2012-03-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 whb678 的回复:]
File.Create(filepath_003);
[/Quote]
创建完了默认是打开的。

FileStream fileStream = File.Create(filepath_003);

接收一下,然后下面用fileStream写。

你连续创建两次filepath_003 肯定冲突了。
mohugomohu 2012-03-06
  • 打赏
  • 举报
回复
string filePath = PublicMethod.GetCurrentPath() + "Cncm_ErrorLog.txt";
using (StreamWriter writer = new StreamWriter(filePath, true, Encoding.Default))
{
writer.Write("\r\n" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "||" + e.Exception.Message);
}
MessageBox.Show(e.Exception.Message, "错误窗口",
MessageBoxButtons.OK, MessageBoxIcon.Error);
EnForGrass 2012-03-06
  • 打赏
  • 举报
回复
nonoliving 2012-03-06
  • 打赏
  • 举报
回复
if (!File.Exists(filepath_003))
{
File.Create(filepath_003); Console.WriteLine("创建文件" + filepath_003 + "!");
}

110,539

社区成员

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

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

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