Save a string to a text file

  • Thread starter Thread starter CCLeasing
  • Start date Start date
C

CCLeasing

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.
 
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.
 
Dustin Campbell said:
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
 
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.
 
Dustin Campbell said:
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);
}
}
 
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.
 
Dustin Campbell said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top