Link to home
Create AccountLog in
Avatar of mhdi
mhdi

asked on

c# file writing - unwanted blank lines being appended.

My code below is suppose to append one new line to a text file.

However it appends the line plus an additional 3-4 blank lines.

using (FileStream fs = new FileStream(file, FileMode.Open, FileSystemRights.AppendData, FileShare.Write, 4096, FileOptions.None))
{
	using (StreamWriter sw = new StreamWriter(fs))
	{
		sw.WriteLine(updateText);
	}
}

Open in new window


Could it have something to do with the 4096 buffer?
or What else could it be?

Thank you
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

The buffer shouldn't affect that.

What is in "updateText"?...that's the more likely culprit.
Avatar of mhdi
mhdi

ASKER

updateText is a string of characters. It doesn't contain any new line characters at all and is quite short.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of mhdi

ASKER

thanks for testing, I will check everything and report back.
You can make it in one line with:

File.AppendAllText(file, updateText + "\r\n");

or

File.AppendAllLines(file, new[]{ updateText });
Avatar of mhdi

ASKER

@jaime_olivares: can I use the filemode and fileshare options with your method?