469,282 Members | 2,404 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Save a string to a text file


I would like to open a text file titled (c:\logs.txt).
If the file doesn't exist i would like to create it.
if it does exist i would like to append to it.

I would like to insert a line into the end of the file.
The line is contained in the variable 'line' which is a string
variable.

once i've done this i would like to close the file.

Nov 7 '06 #1
7 41782
thankyou

Nov 7 '06 #2
I would like to open a text file titled (c:\logs.txt). If the file
doesn't exist i would like to create it. if it does exist i would like
to append to it.

I would like to insert a line into the end of the file.
The line is contained in the variable 'line' which is a string
variable.
once i've done this i would like to close the file.
Look at the System.IO.File class in the MSDN documentation. It has everything
that you want.

private void WriteLinesToFile(string filePath, string[] lines)
{
if (filePath == null || filePath.Length == 0)
return;
if (lines == null || lines.Length == 0)
return;

StreamWriter fileWriter = null;
try
{
if (File.Exists(filePath))
fileWriter = File.AppendText(filePath);
else
fileWriter = File.CreateText(filePath);

foreach (string line in lines)
fileWriter.Write(line);
}
finally
{
if (fileWriter != null)
fileWriter.Close();
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 7 '06 #3


"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>I would like to open a text file titled (c:\logs.txt). If the file
doesn't exist i would like to create it. if it does exist i would like
to append to it.

I would like to insert a line into the end of the file.
The line is contained in the variable 'line' which is a string
variable.
once i've done this i would like to close the file.

Look at the System.IO.File class in the MSDN documentation. It has
everything that you want.

private void WriteLinesToFile(string filePath, string[] lines)
{
if (filePath == null || filePath.Length == 0)
return;
if (lines == null || lines.Length == 0)
return;

StreamWriter fileWriter = null;
try
{
if (File.Exists(filePath))
fileWriter = File.AppendText(filePath);
else
fileWriter = File.CreateText(filePath);

foreach (string line in lines)
fileWriter.Write(line);
}
finally
{
if (fileWriter != null)
fileWriter.Close();
}
}

Best Regards,
Dustin Campbell
Developer Express Inc.

To modify the posted code:

StreamWriter fileWriter = null;
if (File.Exists(filePath))
fileWriter = File.AppendText(filePath);
else
fileWriter = File.CreateText(filePath);
try {
foreach (string line in lines)
fileWriter.Write(line);
} finally {
fileWriter.Close();
}
Notice, no extra checking in the finally block. If we get that far, the
file was opened. Since the file is opened right before the try...catch
block, the fileWriter will contain a reference unless an exception was
closed, in which case, no writer will be opened (and the reference will
still be null).

HTH,
Mythran
Nov 7 '06 #4
To modify the posted code:
>
StreamWriter fileWriter = null;
if (File.Exists(filePath))
fileWriter = File.AppendText(filePath);
else
fileWriter = File.CreateText(filePath);
try {
foreach (string line in lines)
fileWriter.Write(line);
} finally {
fileWriter.Close();
}
Notice, no extra checking in the finally block. If we get that far,
the file was opened. Since the file is opened right before the
try...catch block, the fileWriter will contain a reference unless an
exception was closed, in which case, no writer will be opened (and the
reference will still be null).
Of course, at this point it isn't necessary to set "fileWriter" to null at
all.

StreamWriter fileWriter;
if (File.Exists(filePath)
fileWriter = File.AppendText(filePath);
else
fileWriter = File.CreateText(filePath);

try {
foreach (string line in lines)
fileWriter.Write(line);
}
finally {
fileWriter.Close();
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 7 '06 #5
Dustin Campbell <du*****@no-spam-pleasedevexpress.comwrote:
Of course, at this point it isn't necessary to set "fileWriter" to null at
all.

StreamWriter fileWriter;
if (File.Exists(filePath)
fileWriter = File.AppendText(filePath);
else
fileWriter = File.CreateText(filePath);

try {
foreach (string line in lines)
fileWriter.Write(line);
}
finally {
fileWriter.Close();
}
Given that AppendText creates the file if it doesn't exist (according
to the documentation) you can use:

using (StreamWriter writer = File.AppendText(filePath))
{
foreach (string line in lines)
{
writer.Write (line);
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 7 '06 #6
Given that AppendText creates the file if it doesn't exist (according
to the documentation) you can use:

using (StreamWriter writer = File.AppendText(filePath))
{
foreach (string line in lines)
{
writer.Write (line);
}
}
Perfect. Thanks Jon.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 7 '06 #7


"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>Given that AppendText creates the file if it doesn't exist (according
to the documentation) you can use:

using (StreamWriter writer = File.AppendText(filePath))
{
foreach (string line in lines)
{
writer.Write (line);
}
}

Perfect. Thanks Jon.

Best Regards,
Dustin Campbell
Developer Express Inc.

Aye, now if we all combine all our thought processes for our life works, all
would be good...

:-)

Mythran

Nov 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Andras Gilicz | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.