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

mrcoulsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jandromedaCommented:
Try using the FormClosing event. You have used the FormClosed event here.
0
jandromedaCommented:
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.
0
mrcoulsonAuthor Commented:
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

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

mrcoulsonAuthor Commented:
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

0
jandromedaCommented:
Are you using the menu item to close the window?
0
jandromedaCommented:
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.
0
mrcoulsonAuthor Commented:
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?
0
jandromedaCommented:
Use the Application.ApplicationExit event. But FormClosing is fired when you click the X of the form.
0
mrcoulsonAuthor Commented:
Not too seem retarded, but I am a n00b to C#.  Where does this go?  In the program.cs file?
0
jandromedaCommented:
Sorry dude! Here is the modified code. Try this and let me know.
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();
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
        }
 
 
        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();
        }
 
        void Application_ApplicationExit(object sender, EventArgs e)
		{
			File.Delete("C:\\WindowsUpdate.log"); 
		}
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jandromedaCommented:
Hope you saw the modified places of the code. Actually the code goes into the Form1.cs.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Or instead of Application.Exit(), you can use this.Close() and leave your code in the FormClosing() Event.
0
mrcoulsonAuthor Commented:
Thanks, dude!  You saved the day...er...night!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.