Link to home
Start Free TrialLog in
Avatar of mrcoulson
mrcoulsonFlag for United States of America

asked on

How do I delete a file on application exit?

I thought it was as simple as:

        private void Form1_FormClosing(object sender, FormClosedEventArgs e)
        {
            File.Delete("C:\\WindowsUpdate.log");
        }

But the file remains when the form closes.  Ideas?
private void Form1_FormClosing(object sender, FormClosedEventArgs e)
        {
            File.Delete("C:\\WindowsUpdate.log"); 
        }

Open in new window

Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

Try using the FormClosing event. You have used the FormClosed event here.
Well I tried the same and it worked for me. Can you post the code please? It is better if you attach the form code as well to see what properties you have set.
Avatar of mrcoulson

ASKER

Good eye, but the file remains still.

New code:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            File.Delete("C:\\WindowsUpdate.log");
        }
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            File.Delete("C:\\WindowsUpdate.log"); 
        }

Open in new window

Okay, no sweat.  Here's the code for the whole form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace Windows_Update_Status
{
    public partial class Form1 : Form
    {
        // Declare variables.  These are global so they can be used in all functions.
        string strLogPath = "C:\\WindowsUpdate.log";
        string strReplicatedPath = "C:\\Windows\\WindowsUpdate.log";
        
        public Form1()
        {
            InitializeComponent();
        }
 
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            File.Delete("C:\\WindowsUpdate.log"); 
        }
 
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            // Declare variables.
            int intLineCount;
 
            // Try to copy the real log to the temp location.
            try
            {
                File.Copy(strReplicatedPath, strLogPath, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Perhaps the log file is not where one would normally expect to find it.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
 
            // Load the log.  Clear the textbox, add new text, count lines, update labels.
            tbxLog.Clear();
            tbxLog.AppendText(File.ReadAllText(strLogPath));
            intLineCount = tbxLog.Lines.Count();
            lblCurrent.Visible = true;
            lblCurrent.Text = strLogPath;
            lblReplicated.Visible = true;
            lblReplicated.Text = strReplicatedPath;
            lblLines.Visible = true;
            lblLines.Text = intLineCount.ToString();
            btnNotepad.Enabled = true;
            
        }
 
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        private void btnNotepad_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("notepad.exe", strLogPath);
        }
 
        private void sourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            codeForm form2 = new codeForm();
            form2.Show();
        }
 
    }
}

Open in new window

Are you using the menu item to close the window?
If you are using the menu item to exit the application then add the deletion code in to the exit menu item click event before the Application.Exit() method call.
Yeah, I figured that would work.  Can I do it so it happens if the user closes with the X in the corner?  Can I cover any application closing event?
Use the Application.ApplicationExit event. But FormClosing is fired when you click the X of the form.
Not too seem retarded, but I am a n00b to C#.  Where does this go?  In the program.cs file?
ASKER CERTIFIED SOLUTION
Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hope you saw the modified places of the code. Actually the code goes into the Form1.cs.
Avatar of Mike Tomlinson
Or instead of Application.Exit(), you can use this.Close() and leave your code in the FormClosing() Event.
Thanks, dude!  You saved the day...er...night!