IOException: 进程无法访问文件 'fileName/textFile.txt' 因为它正被另一个进程使用
IOException: The process cannot access the file 'fileName/textFile.txt' because it is being used by another process
我看到了关于这个问题的其他话题,但似乎没有一个能解决我的确切问题。
static void RecordUpdater(string username,int points,string term) //Updates Record File with New Records.
{
int minPoints = 0;
StreamWriter streamWriter = new StreamWriter($@"Record\{term}");
Player playersRecord = new Player(points, username);
List<Player> allRecords = new List<Player>();
StreamReader reader = new StreamReader($@"Record\{term}");
while (!reader.EndOfStream)
{
string[] splitText = reader.ReadLine().Split(',');
Player record = new Player(Convert.ToInt32(splitText[0]), splitText[1]);
allRecords.Add(record);
}
reader.Close();
foreach (var playerpoint in allRecords )
{
if(minPoints > playerpoint.points)
minPoints = playerpoint.points;
}
if (points > minPoints)
{
allRecords.Add(playersRecord);
allRecords.Remove(allRecords.Min());
}
allRecords.Sort();
allRecords.Reverse();
streamWriter.Flush();
foreach (var player in allRecords)
{
streamWriter.WriteLine(player.points + "," + player.username);
}
}
因此,在我 运行 程序并在代码中到达该点后,我收到一条错误消息:
“该进程无法访问文件 'fileName/textFile.txt',因为它正被另一个进程使用。”
您应该在流等一次性对象周围使用 using statement。这将确保对象释放它们持有的所有非托管资源。并且在需要之前不要打开写入器。第一次需要阅读记录时打开编写器是没有意义的
static void RecordUpdater(string username,int points,string term)
{
Player playersRecord = new Player(points, username);
List<Player> allRecords = new List<Player>();
int minPoints = 0;
try
{
using(StreamReader reader = new StreamReader($@"Record\{term}"))
{
while (!reader.EndOfStream)
{
.... load you data line by line
}
}
..... process your data .....
using(StreamWriter streamWriter = new StreamWriter($@"Record\{term}"))
{
... write your data...
}
}
catch(Exception ex)
{
... show a message about the ex.Message or just log everything
in a file for later analysis
}
}
您还应该考虑到,使用文件是最有可能的上下文之一,在这种情况下,您可能会因程序无法控制的外部事件而收到异常。
最好将所有内容都包含在 try/catch 块中,并正确处理异常
我看到了关于这个问题的其他话题,但似乎没有一个能解决我的确切问题。
static void RecordUpdater(string username,int points,string term) //Updates Record File with New Records.
{
int minPoints = 0;
StreamWriter streamWriter = new StreamWriter($@"Record\{term}");
Player playersRecord = new Player(points, username);
List<Player> allRecords = new List<Player>();
StreamReader reader = new StreamReader($@"Record\{term}");
while (!reader.EndOfStream)
{
string[] splitText = reader.ReadLine().Split(',');
Player record = new Player(Convert.ToInt32(splitText[0]), splitText[1]);
allRecords.Add(record);
}
reader.Close();
foreach (var playerpoint in allRecords )
{
if(minPoints > playerpoint.points)
minPoints = playerpoint.points;
}
if (points > minPoints)
{
allRecords.Add(playersRecord);
allRecords.Remove(allRecords.Min());
}
allRecords.Sort();
allRecords.Reverse();
streamWriter.Flush();
foreach (var player in allRecords)
{
streamWriter.WriteLine(player.points + "," + player.username);
}
}
因此,在我 运行 程序并在代码中到达该点后,我收到一条错误消息: “该进程无法访问文件 'fileName/textFile.txt',因为它正被另一个进程使用。”
您应该在流等一次性对象周围使用 using statement。这将确保对象释放它们持有的所有非托管资源。并且在需要之前不要打开写入器。第一次需要阅读记录时打开编写器是没有意义的
static void RecordUpdater(string username,int points,string term)
{
Player playersRecord = new Player(points, username);
List<Player> allRecords = new List<Player>();
int minPoints = 0;
try
{
using(StreamReader reader = new StreamReader($@"Record\{term}"))
{
while (!reader.EndOfStream)
{
.... load you data line by line
}
}
..... process your data .....
using(StreamWriter streamWriter = new StreamWriter($@"Record\{term}"))
{
... write your data...
}
}
catch(Exception ex)
{
... show a message about the ex.Message or just log everything
in a file for later analysis
}
}
您还应该考虑到,使用文件是最有可能的上下文之一,在这种情况下,您可能会因程序无法控制的外部事件而收到异常。
最好将所有内容都包含在 try/catch 块中,并正确处理异常