Problem writing to a text file C#
Here is my program:
protected int CheckExisting(string item_id)
{
StreamReader sr = new StreamReader(@"D:\ItemID.txt");
string line = sr.ReadLine();
while (line != null)
{
if (0 == string.Compare(line, item_id))
return 1;
line = sr.ReadLine();
}
sr.Close();
return 0;
}
protected void WriteNewLog(string item_id)
{
using (StreamWriter sw = F开发者_StackOverflow社区ile.AppendText(@"D:\ItemID.txt"))
{
sw.WriteLine(item_id);
}
}
protected void xHandler(int num)
{
for(int i= 0; i< num; i++)
if (0 == CheckExisting(item_id))
{
WriteNewLog(item_id);
}
}
When o run the program, an exception unhandled occur: "The process cannot access the file 'D:\ItemID.txt' because it is being used by another process." Can u guys help me fix that? Thanks so much!
If this executes:
if (0 == string.Compare(line, item_id))
return 1;
then you won't close your StreamReader
. Use a using
block when reading as well as writing.
Additionally:
- I'd suggest using
bool
instead of an integer to indicate yes/no results - I'd use a simple equality check instead of calling
Compare
and checking the results against0
- If you're using .NET 4,
File.ReadLines
is a simpler way of reading lines - LINQ makes it easier to implement
CheckExisting
to start with - The name
xHandler
doesn't follow .NET naming conventions - Underscores in parameter names don't follow .NET naming conventions
- I would suggest using braces for clarity everywhere, even where they're not strictly necessary
- I would put the constants in comparisons at the end of the comparison instead of at the start; I believe most people find that more readable
- There's no indication that the
CheckExisting
andWriteNewLog
methods need to be protected rather than private - You're not actually using your
i
variable inxHandler
Here's the implementation I'd use:
const string FileName = @"D:\ItemID.txt";
private bool CheckExisting(string itemId)
{
return File.ReadLines(FileName)
.Contains(itemId);
}
private void WriteNewLog(string itemId)
{
using (TextWriter writer = File.AppendText(FileName))
{
writer.WriteLine(itemId);
}
}
// Adjust name appropriately
protected void FooHandler(int num)
{
for (int i = 0; i < num; i++)
{
// Probably use i here somewhere?
if (!CheckExisting(itemId))
{
WriteNewLog(itemId);
}
}
}
try putting the stream reader in a using so that its dispose is called.
right now your code will return before sr.Close() is called.
You should either use using
or try/finally
to make sure your recourse is closed, in your code the exception is thrown because of return 1;
the method will exit without closing the stream and so you getting file in use by another process. example on try/finally
StreamReader sr = new StreamReader(@"D:\ItemID.txt");
try
{
string line = sr.ReadLine();
while (line != null)
{
if (0 == string.Compare(line, item_id))
return 1;
line = sr.ReadLine();
}
}
finally
{
sr.Close();
}
return 0;
You are not closing the file when you return 1 from CheckExisting. Also use the using (StringReader rdr = ...)
.
Because the file is still locked open by your process, it cannot be opened again. That's why you see this Exception: The process cannot access the file 'D:\ItemID.txt' because it is being used by another process
.
精彩评论