QUOTE(zakary @ 1 Jul, 2008 - 12:12 PM)

you can modify that code so where it has
csharp
if (File.Exists(path))
{
File.Delete(path);
}
you can use
csharp
if (File.Exists(path))
{
File.Open(path);
}
and then you can add text to that open file
Thanks zakary for your helps. I made changes to my code from my first post.
CODE
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace TestLogFile
{
public class TestLog
{
private FileStream fStream;
private StreamWriter strmWriter;
private string filePath;
//Function Name: Constructor.
//Purpose: To create new log file. If log file is already exist then it
// will get deleted first before create new log file.
//Parameter: The function that references this DLL have to provide the path
// to where the file can be written.
public TestLog(string path)
{
filePath = path;
//Delete file if it exists.
if (File.Exists(filePath))
{
File.Delete(filePath);
}
//Create new test log file.
fStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.ReadWrite);
//Create a writer and also open a file.
strmWriter = new StreamWriter(fStream);
//Close out the file and StreamWriter.
EndLog();
}
//
//
public void CreateLog()
{
}
//Function Name: LogString.
//
//
public void LogString(bool writeSameLine, string str)
{
if(writeSameLine == true)
{
strmWriter.Write(str);
}
else
{
strmWriter.WriteLine(str);
}
}
//Function Name: LogChar.
//
//
public void LogChar(bool writeSameLine, char ch)
{
if(writeSameLine == true)
{
strmWriter.Write(ch);
}
else
{
strmWriter.WriteLine(ch);
}
}
//Function Name: LogBoolen.
//Purpose: To write True or False or other case such is 0 or 1.
//
public void LogBoolen(bool writeSameLine, char writeType, bool TrueFalse)
{
}
//Function Name: LogInt.
//
//
public void LogInt(int numb)
{
}
//Function Name: OpenLog.
//
//
public void OpenLog()
{
if (File.Exists(filePath))
File.Open(filePath, FileMode.Append);
}
//Function Name: EndLog.
//
//
public void EndLog()
{
strmWriter.Close();
fStream.Close();
}
}
}
Here is my console app that call the code above.
CODE
using System;
using System.Collections.Generic;
using System.Text;
using TestLogFile;
namespace TestingTestingCallingTestLog
{
class Program
{
public static string FilePath = @"C:\\Results\\TestLog.txt";
static void Main(string[] args)
{
TestLog testClass = new TestLog(FilePath);
testClass.LogString(false, "here I am testing testing testing");
testClass.EndLog();
testClass.LogChar(true, 'a');
testClass.EndLog();
}
}
}
As you can see my code above, it will close the streamWriter and file everytime I finish writing, so so far I'm able to open file, but when I try to logString() again, it gives me error since my streamWriter is already closed. I can avoid this if I don't close streamWriter away, but I would like for it to close every writing session.
I want to open streamWriter in my function called openLog(), but I haven't found the way how to do that yet.
This post has been edited by turtleC++: 1 Jul, 2008 - 12:32 PM