I created a little class in order to create logs. This class use streamwriter functions in order to do this.
I write a log, and after closed the log, i would like to re-open after, in order to append some datas.
I tried severals tips, and...i always have an exception who tell me " The file is used by another process".
Nevertheless, i use the close(), but, i always had this exception.
Here is my class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace TEST
{
public class CLog
{
private StreamWriter myWriter;
public string Name
{
set { Name = value; }
get
{
FileInfo myFile = new FileInfo(CompleteFileName);
return myFile.Name;
}
}
public string Directory
{
set { Directory = value; }
get
{
FileInfo myFile = new FileInfo(CompleteFileName);
return myFile.Directory.ToString();
}
}
public string CompleteFileName { set; get; }
public CLog(string _CompleteFileName)
{
CompleteFileName = _CompleteFileName;
}
public bool CreateLog()
{
try
{
myWriter = new StreamWriter(CompleteFileName);
return true;
}
catch (Exception ex )
{
return false;
}
}
public bool AppendTextToFile(string strText)
{
try
{
using (System.IO.StreamWriter sw = System.IO.File.AppendText(CompleteFileName))
{
sw.WriteLine(strText);
}
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool WriteLine(string strLine)
{
try
{
myWriter.WriteLine(strLine);
return true;
}
catch (Exception ex)
{
return false;
}
}
public bool SaveFile()
{
try
{
myWriter.Close();
myWriter.Dispose();
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}
Please look at the AppendTextToFile.
Here is the use :
monLog = new CLog("C:\\TEST.TXT");
if (monLog.CreateLog())
{
monLog.WriteLine("");
monLog.WriteLine("******");
monLog.WriteLine("Some data");
monLog.WriteLine("******");
monLog.SaveFile();
...
....
monLog.AppendTextToFile("** my AppendedData***);
monLog.SaveFile();
}
Anyone know why i have this exception and how solve it ?
Thanks a lot :)