【问题标题】:Delete a file after process is close. C# windows forms进程关闭后删除文件。 C# 窗体
【发布时间】:2018-12-13 15:43:29
【问题描述】:

我有一个 Windows 窗体应用程序,它在应用程序运行时使用临时文件,并在主窗体关闭后将其删除。问题是,如果我通过终止进程从任务管理器中关闭程序,文件不会被删除。

有没有办法在进程以任何方式而不是表单关闭时自动删除文件?

【问题讨论】:

  • 没有。因为当你杀死进程时,你的程序不会进入程序的关闭过程
  • FileOptions.DeleteOnClose?
  • 如果你的进程被操作系统终止(当你使用任务管理器时会发生这种情况),你什么也做不了(因为 - 好吧 - 你被终止了)。这就像问“如果有人拔掉电源插头,如何删除该文件?”
  • 把文件放到TEMP文件夹里,让os操心? Path.GetTempFileName();

标签: c# winforms process


【解决方案1】:

您可以使用FileOptions.DeleteOnClose,如FileOptions Enum 文档中所述,用于此用例。

DeleteOnClose 的文档说明:

表示文件不再使用时自动删除。

【讨论】:

  • 这个也可以在打开文件的时候使用吗?在我的情况下,它的 excel 文件由 devexpress gridview 在 temp 文件夹中生成,生成后我打开它们。我想在用户关闭它们时删除它们
  • @GuidoG “当用户关闭它们时”是什么意思?用户是在您创建文件后打开文件,还是您是最后一个处理文件的人?
  • 我的应用程序在 temp 文件夹中创建了一个 excel 文件,然后将其打开。现在当用户完成并关闭它时,如果它被删除就好了
  • 嗯,这就是我的想法。而不是调用 excel,我会调用自己的应用程序来启动 excel,并监视文件,以便在不再使用时将其删除
  • 我这个懒惰的程序员只是希望有更简单的方法
【解决方案2】:

另一个解决方案(很容易理解)是使用一个帮助程序,如果你的进程存在,我在我的很多程序中使用这个解决方案来监督(我不是这个想法的作者,不记得我在哪里发现了这个想法,我刚刚修改了代码):您可以删除退出事件中的临时文件..

    static void Main(string[] args)
    {
        //Main App Process.
        if (args.Length == 0)
        {
            //Saves current process info to pass on command line.
            main = Process.GetCurrentProcess();
            mainProcessID = main.Id;

            //Initializes the helper process
            checker = new Process();
            checker.StartInfo.FileName = main.MainModule.FileName;
            checker.StartInfo.Arguments = mainProcessID.ToString();

            checker.EnableRaisingEvents = true;
            checker.Exited += new EventHandler(checker_Exited);

            //Launch the helper process.
            checker.Start();

            Application.Run(new MainForm()); // your winform app
        }
        else //On the helper Process
        {
            main = Process.GetProcessById(int.Parse(args[0]));

            main.EnableRaisingEvents = true;
            main.Exited += new EventHandler(main_Exited);

            while (!main.HasExited)
            {
                Thread.Sleep(1000); //Wait 1 second. 
            }

            //Provide some time to process the main_Exited event. 
            Thread.Sleep(2000);
        }
    }

    //Double checks the user not closing the checker process.
    static void checker_Exited(object sender, EventArgs e)
    {           
        //This only checks for the task manager process running. 
        //It does not make sure that the app has been closed by it. But close enough.
        //If you can think of a better way please let me know.
        if (Process.GetProcessesByName("taskmgr").Length != 0)
        {
            MessageBox.Show("Task Manager killed helper process.");

            //If you like you could kill the main app here to. 
            //main.Kill();
        }
    }

    //Only gets to run on the checker process. The other one should be dead. 
    static void main_Exited(object sender, EventArgs e)
    {
        //This only checks for the task manager process running. 
        //It does not make sure that the app has been closed by it. But close enough.
        //If you can think of a better way please let me know.

        if (Process.GetProcessesByName("taskmgr").Length != 0)
        {
            MessageBox.Show("Task Manager killed my application.");
        }
    }

必须有更好的方法来检查kill,可能是在任务管理器上捕获消息,或者挂钩到任务管理器。但是,解决方案变得更加复杂

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多