读取和写入文件 C# 时共享冲突 IOException [英] Sharing violation IOException while reading and writing to file C#

查看:36
本文介绍了读取和写入文件 C# 时共享冲突 IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

public static TextWriter twLog = null;
private int fileNo = 1;
private string line = null;

TextReader tr = new StreamReader("file_no.txt");
TextWriter tw = new StreamWriter("file_no.txt");
line = tr.ReadLine();
if(line != null){
    fileNo = int.Parse(line);
    twLog = new StreamWriter("log_" + line + ".txt");
}else{
    twLog = new StreamWriter("log_" + fileNo.toString() + ".txt");  
}
System.IO.File.WriteAllText("file_no.txt",string.Empty);
tw.WriteLine((fileNo++).ToString());
tr.Close();
tw.Close();
twLog.Close();

它抛出这个错误:

IOException:在路径 C:UsersWater Simulationfile_no.txt 上共享冲突

IOException: Sharing violation on path C:UsersWater Simulationfile_no.txt

我想要做的只是打开一个名为 log_x.txt 的文件,然后从 file_no.txt 文件中取出x".如果 file_no.txt 文件为空,则制作日志文件的名称 log_1.txt 并写入fileNo+ 1" 到 file_no.txt.新程序启动后,新的日志文件名必须是 log_2.txt.但我收到此错误,我不明白我做错了什么.感谢帮助.

What i'm trying to do is just open a file with log_x.txt name and take the "x" from file_no.txt file.If file_no.txt file is empty make log file's name log_1.txt and write "fileNo + 1" to file_no.txt.After a new program starts the new log file name must be log_2.txt.But i'm getting this error and i couldn't understand what am i doing wrong.Thanks for help.

推荐答案

好吧,您正在尝试打开文件 file_no.txt 用于阅读 用于编写使用单独的流.这可能不起作用,因为文件将被读取流锁定,因此无法创建写入流并出现异常.

Well, you're trying to open the file file_no.txt for reading and for writing using separate streams. This may not work as the file will be locked by the reading stream, so the writing stream can't be created and you get the exception.

一种解决方案是先读取文件,关闭流,然后在增加 fileNo 后写入文件.这样文件一次只能打开一次.

One solution would be to read the file first, close the stream and then write the file after increasing the fileNo. That way the file is only opened once at a time.

另一种方法是为这样的读写访问创建一个文件流:

Another way would be to create a file stream for both read and write access like that:

FileStream fileStream = new FileStream(@"file_no.txt", 
                                       FileMode.OpenOrCreate, 
                                       FileAccess.ReadWrite, 
                                       FileShare.None);

这个问题的公认答案似乎有也是一个很好的解决方案,即使我假设您不想允许共享读取.

The accepted answer to this question seems to have a good solution also, even though I assume you do not want to allow shared reads.

可能的替代解决方案
我了解您希望在程序启动时创建唯一的日志文件.另一种方法是这样的:

Possible alternate solution
I understand you want to create unique log files when your program starts. Another way to do so would be this:

int logFileNo = 1;
string fileName = String.Format("log_{0}.txt", logFileNo);

while (File.Exists(fileName))
{
    logFileNo++;
    fileName = String.Format("log_{0}.txt", logFileNo);
}

这会增加数字,直到找到不存在日志文件的文件编号.缺点:如果您有 log_1.txtlog_5.txt,下一个文件将不是 log_6.txt 而是 log_2.txt.

This increases the number until it finds a file number where the log file doesn't exist. Drawback: If you have log_1.txt and log_5.txt, the next file won't be log_6.txt but log_2.txt.

为了克服这个问题,您可以使用掩码 log_*.txt 枚举目录中的所有文件,并通过执行一些字符串操作来找到最大数量.

To overcome this, you could enumerate all the files in your directory with mask log_*.txt and find the greatest number by performing some string manipulation.

可能性是无限的:-D

这篇关于读取和写入文件 C# 时共享冲突 IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆