Click here to Skip to main content
16,001,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My application is designed using C# winforms .net 4.0.

My application creates a log file of its own and this is programmatically

created means application itself creating it on setup. The folder structure is AppData->MyApp->MyApp1->logging.log.

When the application is uninstalled, only this file has to get deleted.

How can i achieve this? Please someone help me

C#
 private const string LOG_FILE = "logging";
 private const string LOG_FILE_EXT = ".log";
 m_strGeneralFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Config.APPDATA_PATH;


public void CheckLogFile()
    {
        string file = Config.LOG_FILE;
        m_strLogfile = Config.LOG_FILE + Config.LOG_FILE_EXT;


        if (File.Exists(m_strGeneralFilePath + file + Config.LOG_FILE_EXT))
        {
            long logFileStreamLength = 0;
            using (Stream logFileStream = new FileStream(m_strGeneralFilePath + file + Config.LOG_FILE_EXT, FileMode.Open, FileAccess.Read))
            {
                logFileStreamLength = logFileStream.Length;
                logFileStream.Flush();
                logFileStream.Close();
            }
            //configure a default value if user does not specify the value
            if (string.IsNullOrEmpty(logSize))
            {
                logSize = "1000000";//1MB will the default log file size
            }
            if (logFileStreamLength > Convert.ToInt32(logSize)) // approx. 1MB 1.000.000
            {
                for (int i = 9; i > 0; i--)
                {
                    if (i == 9)
                    {
                        if (File.Exists(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT))
                        {
                            File.Delete(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT);
                        }
                        continue;
                    }
                    if (File.Exists(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT))
                    {
                        System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + i + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + (i + 1) + Config.LOG_FILE_EXT);
                    }
                }
                System.IO.File.Move(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT, m_strGeneralFilePath + Config.LOG_FILE + "1" + Config.LOG_FILE_EXT);

                using (FileStream fs = File.Create(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT))
                {
                    fs.Flush();
                    fs.Close();
                }

            }
        }
        else
        {
            using (FileStream fs = File.Create(m_strGeneralFilePath + Config.LOG_FILE + Config.LOG_FILE_EXT))
            {
                fs.Flush();
                fs.Close();
            }
        }


    }
Posted

1 solution

In the setup project add the log file with the same name as you are creating in the code. Specify the path where the file will be created.

On uninstalling the application the log file will be deleted automatically
 
Share this answer
 
Comments
Roopini Nagaraj 22-Oct-13 1:55am    
@Madhu Nair-How can i add this to setup project? does that override the existing?
there is a config file also in the same folder where i want to add this.....
for example is the folder structure. AppData->MyApp->Myapp1,MyApp2.
in the same folder app.dat and temperory log.txt is also created. Doesn't this override the programmatically created log file (as mentioned above in the code) to the setup created log?
Madhu Nair 22-Oct-13 7:28am    
Create a special folder where you want to save the log file and add a blank app.dat in this folder. once setup is installed, this file will be created automatically on the system

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900