Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [Help] C# delete file unauthorized access exception  (Read 11409 times)

0 Members and 1 Guest are viewing this topic.

DaftHacker

    Topic Starter


    Rookie

  • I am Daft
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 8
[Help] C# delete file unauthorized access exception
« on: July 07, 2013, 01:13:58 AM »
Im trying to make an updater for my application but I keep getting exceptions when trying to delete the file or download the new file.

unauthorized access exception for file delete or webclient error for downloading new file. Ive tried setting the program to run as admin, the program that needs deleting is all normal, nothing set to read only or anything else that would make the update throw an exception. The file set to update is another app made in c#

This is what is suppose to happen:

Login System - prompt to update
Login System - download updater
Login System - exit or have update program exit
Updater - make sure all processes with name: Login System are not running
Updater - Delete Login System - Error - File access exception
Updater - Download latest version - Sometimes error usually if the file doesn't delete
Updater - start Login System
Updater - Exit
Login System - Check for updater.exe and delete on startup


Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;

namespace Update
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //Set program name and program download.
        string ProgramName = "Login System.exe";
        string DownloadLocation = "";

        WebClient WC = new WebClient();

        private void Form1_Load(object sender, EventArgs e)
        {
            //Check if the program is in the same folder as the update
            if (File.Exists(ProgramName))
            {

                //Try to stop all running processes
                    Process[] RunningProcess = Process.GetProcessesByName(ProgramName.Replace(".exe", ""));
                    foreach (Process CurrentProcess in RunningProcess)
                    {
                        if (!CurrentProcess.HasExited)
                        {
                            try
                            {
                                CurrentProcess.CloseMainWindow();
                                CurrentProcess.Close();
                                CurrentProcess.Kill();
                            }
                            catch { }
                        }
                    }
            }
            else
            {
                MessageBox.Show("Unable to Access: " + ProgramName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }

            //Delete old program
            try
            {             
                if (File.Exists(ProgramName))
                {
                    File.Delete(ProgramName);
                }
            }
            catch (Exception EX)
            {
                MessageBox.Show("Delete File " + EX.Message, "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }

            //Download new updated program
            try
            {
                WC.DownloadFile(DownloadLocation, ProgramName);
            }
            catch (Exception EX)
            {
                MessageBox.Show("Download file " + EX.Message, "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }

            //Start program
            try
            {
                if (File.Exists(ProgramName))
                {
                    Process.Start(ProgramName);
                }
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message, "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }

            //Close Updater
            this.Close();
        }
    }
}

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: [Help] C# delete file unauthorized access exception
« Reply #1 on: July 07, 2013, 01:58:01 AM »
You aren't passing a URI to the DownloadFile method, it's an empty string.

I was trying to dereference Null Pointers before it was cool.

DaftHacker

    Topic Starter


    Rookie

  • I am Daft
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 8
Re: [Help] C# delete file unauthorized access exception
« Reply #2 on: July 07, 2013, 02:06:18 AM »
You aren't passing a URI to the DownloadFile method, it's an empty string.
Yeah I know, I removed my dropbox link. It just points to a direct download of my application.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: [Help] C# delete file unauthorized access exception
« Reply #3 on: July 07, 2013, 12:14:57 PM »
Two things:

Process.CloseMainWindow and Process.Kill is asynchronous. You would need to call .WaitForExit() if you don't want your code flow to proceed until the Application actually exits.

However, Close() is for closing the handles itself; after the Close() call the Process instance is no longer valid, since the resources used locally for handling process information were cleaned up, so the Kill() call doesn't do anything.

You probably shouldn't be using Kill anyway. CloseMainWindow() and then wait for it to exit should work.

I was trying to dereference Null Pointers before it was cool.

DaftHacker

    Topic Starter


    Rookie

  • I am Daft
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 8
Re: [Help] C# delete file unauthorized access exception
« Reply #4 on: August 03, 2013, 11:49:34 PM »
Two things:

Process.CloseMainWindow and Process.Kill is asynchronous. You would need to call .WaitForExit() if you don't want your code flow to proceed until the Application actually exits.

However, Close() is for closing the handles itself; after the Close() call the Process instance is no longer valid, since the resources used locally for handling process information were cleaned up, so the Kill() call doesn't do anything.

You probably shouldn't be using Kill anyway. CloseMainWindow() and then wait for it to exit should work.
I finally had the time to fix this, thanks.