?
Solved

How do I delete a file on application exit?

Posted on 2008-11-08
13
Medium Priority
?
1,988 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:mrcoulson
  • 7
  • 5
13 Comments
 
LVL 12

Expert Comment

by:jandromeda
ID: 22914899
Try using the FormClosing event. You have used the FormClosed event here.
0
 
LVL 12

Expert Comment

by:jandromeda
ID: 22914912
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
 

Author Comment

by:mrcoulson
ID: 22914915
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mrcoulson
ID: 22914917
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
 
LVL 12

Expert Comment

by:jandromeda
ID: 22914943
Are you using the menu item to close the window?
0
 
LVL 12

Expert Comment

by:jandromeda
ID: 22914954
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
 

Author Comment

by:mrcoulson
ID: 22914961
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
 
LVL 12

Expert Comment

by:jandromeda
ID: 22914983
Use the Application.ApplicationExit event. But FormClosing is fired when you click the X of the form.
0
 

Author Comment

by:mrcoulson
ID: 22915014
Not too seem retarded, but I am a n00b to C#.  Where does this go?  In the program.cs file?
0
 
LVL 12

Accepted Solution

by:
jandromeda earned 2000 total points
ID: 22915033
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
 
LVL 12

Expert Comment

by:jandromeda
ID: 22915035
Hope you saw the modified places of the code. Actually the code goes into the Form1.cs.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22915044
Or instead of Application.Exit(), you can use this.Close() and leave your code in the FormClosing() Event.
0
 

Author Comment

by:mrcoulson
ID: 22915068
Thanks, dude!  You saved the day...er...night!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question