如何在安装卸载应用程序/安装应用程序时删除文件? [英] How can I delete a file when setup uninstalling application/Installing application?

查看:100
本文介绍了如何在安装卸载应用程序/安装应用程序时删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是使用C#winforms .net 4.0设计的。



我的应用程序创建了一个自己的日志文件,这是以编程方式



创建意味着应用程序本身在设置时创建它。文件夹结构是AppData-> MyApp-> MyApp1-> logging.log。



卸载应用程序时,只有此文件必须删除。



我怎样才能做到这一点?请有人帮助我



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

 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();
            }
        }


    }

推荐答案

在设置中project添加日志文件,其名称与您在代码中创建的名称相同。指定创建文件的路径。



卸载应用程序后,日志文件将自动删除
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


这篇关于如何在安装卸载应用程序/安装应用程序时删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆