0

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 :)

Walter Fabio Simoni
  • 5,671
  • 15
  • 55
  • 80
  • Your logger has a strange interface.. other than that, your code runs fine on my machine. – Simon Whitehead Jul 11 '13 at 10:37
  • 1
    The only thing i can think of is that an exception is thrown in one of those methods and because you are not disposing the `StreamWriter` in any exception handler this will probably cause a handle to the file to remain open, so try figure out if any method is throwing an exception and add code in each exception handler to close and dispose the `StreamWriter` correctly. – Ibrahim Najjar Jul 11 '13 at 10:40
  • here is somehing you can look http://stackoverflow.com/questions/14458003/streamwriter-streamreader-file-in-use-by-another-process – Rajeev Bera Jul 11 '13 at 10:54
  • Don't know why but even in adding the close() dans dispose() i have the exception ! – Walter Fabio Simoni Jul 11 '13 at 11:10

0 Answers0