T
tshad
I have a class that write text to a textfile (csv file).
The problem is that it drops off the last 10 lines or so.
I know all the data is getting written to the stream but the it is like the
last bit of the buffer is not getting written out. I close the writer in
reverse order from what I opened it with - so I don't think that is the
problem.
Anyone know what would cause that?
The last thing I do in my code is the .close().
Here is my class. Am I missing anything?
****************************************************
using System;
using System.IO;
using System.Data;
using System.Text;
namespace AutoUPS
{
public class CsvWriter
{
private StringBuilder stringOut = null;
private StreamWriter csvWriter = null;
private string fileName;
bool newLine = true;
FileStream fs = null;
StreamWriter csvFileWriter = null;
public CsvWriter(string fileName)
{
this.fileName = fileName;
fs = new FileStream(fileName, FileMode.Create,
FileAccess.ReadWrite);
csvFileWriter = new StreamWriter(fs);
}
~CsvWriter()
{
//csvWriter.Close();
}
public void Close()
{
if(csvWriter != null)
csvWriter.Close();
if (fs != null)
fs.Close();
}
public void WriteToString(string input)
{
if(stringOut == null)
stringOut = new StringBuilder();
WriteToString(input, true);
}
public void WriteToString(string input, bool quoteall)
{
if (!newLine)
stringOut.Append(",");
if (quoteall || input.IndexOfAny("\",\x0A\x0D".ToCharArray())
else
stringOut.Append(input);
newLine = false;
}
public void WriteEOL()
{
stringOut.Append(Environment.NewLine);
newLine = true;
}
public void WriteToStream()
{
if (stringOut != null)
{
csvFileWriter.Write(stringOut.ToString());
stringOut = null;
}
}
public string GetText()
{
return stringOut.ToString();
}
}
}
*******************************************************
Thanks,
Tom
The problem is that it drops off the last 10 lines or so.
I know all the data is getting written to the stream but the it is like the
last bit of the buffer is not getting written out. I close the writer in
reverse order from what I opened it with - so I don't think that is the
problem.
Anyone know what would cause that?
The last thing I do in my code is the .close().
Here is my class. Am I missing anything?
****************************************************
using System;
using System.IO;
using System.Data;
using System.Text;
namespace AutoUPS
{
public class CsvWriter
{
private StringBuilder stringOut = null;
private StreamWriter csvWriter = null;
private string fileName;
bool newLine = true;
FileStream fs = null;
StreamWriter csvFileWriter = null;
public CsvWriter(string fileName)
{
this.fileName = fileName;
fs = new FileStream(fileName, FileMode.Create,
FileAccess.ReadWrite);
csvFileWriter = new StreamWriter(fs);
}
~CsvWriter()
{
//csvWriter.Close();
}
public void Close()
{
if(csvWriter != null)
csvWriter.Close();
if (fs != null)
fs.Close();
}
public void WriteToString(string input)
{
if(stringOut == null)
stringOut = new StringBuilder();
WriteToString(input, true);
}
public void WriteToString(string input, bool quoteall)
{
if (!newLine)
stringOut.Append(",");
if (quoteall || input.IndexOfAny("\",\x0A\x0D".ToCharArray())
stringOut.Append("\"" + input.Replace("\"", "\"\"") + "\"");
else
stringOut.Append(input);
newLine = false;
}
public void WriteEOL()
{
stringOut.Append(Environment.NewLine);
newLine = true;
}
public void WriteToStream()
{
if (stringOut != null)
{
csvFileWriter.Write(stringOut.ToString());
stringOut = null;
}
}
public string GetText()
{
return stringOut.ToString();
}
}
}
*******************************************************
Thanks,
Tom