Reputation: 21
I am running into an issue that has proven to be difficult to diagnose. I created an auto-updating feature in the past so that people could download updates seamlessly within the application. The code that I had used in the past worked fine but when I implemented it into my new application, I get this error. I have tried things such as setting the attributes to normal and it still doesn't seem to work. The download of the updated version works, the renaming of each file works, but when it tries to delete the old version, that is when the error occurs. Any help would be greatly appreciated!
Code:
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
namespace ezCPU
{
public partial class ezCPU : Form
{
//Create objects of each class that is needed
CPU cpu = new CPU();
GPU gpu = new GPU();
Motherboard mb = new Motherboard();
Memory mem = new Memory();
//Variable to hold the new app version
string newVersion = "";
//Constructor
public ezCPU()
{
InitializeComponent();
}
//On load, do this
private void ezCPU_Load(object sender, EventArgs e)
{
this.Text = this.Text + " - v" + Application.ProductVersion;
this.abVersion.Text = "Version: " + Application.ProductVersion;
//Call CPU class and display the results
cpu.GetCPUInfo();
DisplayCPUStats();
//Call the GPU class and display the results
gpu.GetGPUInfo();
DisplayGPUStats();
//Call the motherboard class and display the results
mb.GetMBInfo();
DisplayMBStats();
//Call the memory class and display the results
mem.GetRAMInfo();
DisplayMemoryStats();
}
//Pulls the information from the CPU class to display it on the form
public void DisplayCPUStats()
{
txtCPUName.Text = cpu.cpuName;
if (cpu.cpuManufacturer.Contains("Intel"))
{
txtCPUManufacturer.Text = "Genuine Intel";
}
else
{
txtCPUManufacturer.Text = cpu.cpuManufacturer;
}
txtCores.Text = cpu.cpuCores;
txtThreads.Text = cpu.cpuThreads;
txtMaxSpeed.Text = cpu.ConvertClockSpeed(cpu.cpuMaxSpeed);
txtCurrentSpeed.Text = cpu.ConvertClockSpeed(cpu.cpuCurrentSpeed);
txtCaption.Text = cpu.cpuCaption;
txtStatus.Text = cpu.cpuStatus;
txtArchitecture.Text = cpu.GetArchitecture(Convert.ToInt16(cpu.cpuArchitecture));
}
//Pulls the information from the GPU class to display it on the form
public void DisplayGPUStats()
{
txtGPUName.Text = gpu.gpuName;
txtGPUManufacturer.Text = gpu.gpuManufacturer;
txtGPUVideoMode.Text = gpu.gpuVideoMode;
txtGPURefresh.Text = gpu.gpuRefreshRate + " hertz";
txtGPUStatus.Text = gpu.gpuStatus;
txtGPUDriverVersion.Text = gpu.gpuDriverVersion;
txtGPUDriverDate.Text = gpu.gpuDriverDate;
}
//Pulls the information from the Motherboard class to display it on the form
public void DisplayMBStats()
{
//Motherboard information
txtMBManufacturer.Text = mb.mbManufacturer;
txtMBModel.Text = mb.mbModel;
txtMBSerial.Text = mb.mbSerial;
txtMBBusType.Text = mb.mbBusType;
txtMBStatus.Text = mb.mbStatus;
//BIOS information
txtBIOSVersion.Text = mb.biosVersion;
txtBIOSDate.Text = mb.biosDate;
txtBIOSBrand.Text = mb.biosManufacturer;
}
//Pulls the information from the Memory class to display it on the form
public void DisplayMemoryStats()
{
//RAM information
txtRAMSize.Text = mem.BytesToGB(mem.ramSize);
txtRAMManufacturer.Text = mem.ramManufacturer;
txtRAMType.Text = mem.GetRAMType(Convert.ToInt16(mem.ramType));
txtRAMFrequency.Text = String.Format("{0:n1}", Convert.ToInt16(mem.ramFrequency)) + " MHz";
}
//Visit the GitHub repo on click
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/ioschris/ezCPU");
}
//Check for update and download if it exists
public void DownloadUpdate()
{
//URL of the updated file
string url = "http://www.chrisharrisdev.com/ezcpu/ezCPU.exe";
//Declare new WebClient object
WebClient wc = new WebClient();
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(new Uri(url), Application.StartupPath + "/ezCPU(1).exe");
}
//When the download completes, show the message box and restart the application
void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
//Show a message when the download has completed
MessageBox.Show("ezCPU is now up-to-date!\n\nThe application will now restart!", "ezCPU - Update Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Restart();
}
//Create method to check for an update
public void GetUpdate()
{
//Declare new WebClient object
WebClient wc = new WebClient();
string textFile = wc.DownloadString("http://www.chrisharrisdev.com/ezcpu/ezcpu_version.txt");
newVersion = textFile;
//If there is a new version, call the DownloadUpdate method
if (newVersion != Application.ProductVersion)
{
MessageBox.Show("An update is available!\n\nClick OK to download and restart!", "ezCPU - Update Available", MessageBoxButtons.OK, MessageBoxIcon.Information);
DownloadUpdate();
}
else
{
MessageBox.Show("ezCPU is up-to-date!\n\nThere is not an update that needs to be applied!", "ezCPU - Up-to-Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//When the app restarts, rename the updated file, rename the original file, and delete the old version
private void ezCPU_FormClosed(object sender, FormClosedEventArgs e)
{
//This renames the original file so any shortcut works and names it accordingly after the update
if (System.IO.File.Exists(Application.StartupPath + "/ezCPU(1).exe"))
{
System.IO.File.Move(Application.StartupPath + "/ezCPU.exe", Application.StartupPath + "/ezCPU(2).exe");
System.IO.File.Move(Application.StartupPath + "/ezCPU(1).exe", Application.StartupPath + "/ezCPU.exe");
System.IO.File.Delete(Application.StartupPath + "/ezCPU(2).exe");
}
}
//Check for updates
private void button1_Click(object sender, EventArgs e)
{
GetUpdate();
}
}
}
Error: https://i.gyazo.com/e465b9071bed32e3f4ba065c121e45a5.png
Error Text:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.UnauthorizedAccessException: Access to the path 'C:\Users\crhar\OneDrive\Desktop\ezCPU\ezCPU(2).exe' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at System.IO.File.Delete(String path)
at ezCPU.ezCPU.ezCPU_FormClosed(Object sender, FormClosedEventArgs e)
at System.Windows.Forms.Form.OnFormClosed(FormClosedEventArgs e)
at System.Windows.Forms.Form.RaiseFormClosedOnAppExit()
at System.Windows.Forms.Application.ExitInternal()
at System.Windows.Forms.Application.Restart()
at ezCPU.ezCPU.wc_DownloadFileCompleted(Object sender, AsyncCompletedEventArgs e)
at System.Net.WebClient.OnDownloadFileCompleted(AsyncCompletedEventArgs e)
at System.Net.WebClient.DownloadFileOperationCompleted(Object arg)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4075.0 built by: NET48REL1LAST
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
ezCPU
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/crhar/OneDrive/Desktop/ezCPU/ezCPU.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4042.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4001.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3752.0 built by: NET48REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3752.0 built by: NET48REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4110.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3752.0 built by: NET48REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Management
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3752.0 built by: NET48REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Management/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Management.dll
----------------------------------------
System.Deployment
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3752.0 built by: NET48REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Deployment/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Deployment.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Upvotes: 0
Views: 215
Reputation: 21
So, after being absolutely brain dead, I have figured out why it didn't work. Previously, I added the deletion code on the load event but the version on my server didn't have that same updated code. So, when it would start the new version, the code was not being called... This is the new code.
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
namespace ezCPU
{
public partial class ezCPU : Form
{
//Create objects of each class that is needed
CPU cpu = new CPU();
GPU gpu = new GPU();
Motherboard mb = new Motherboard();
Memory mem = new Memory();
//Variable to hold the new app version
string newVersion = "";
//Constructor
public ezCPU()
{
InitializeComponent();
}
//On load, do this
private void ezCPU_Load(object sender, EventArgs e)
{
if (System.IO.File.Exists(Application.StartupPath + "/ezCPU(2).exe"))
{
Console.WriteLine("EXISTS");
System.IO.File.Delete(Application.StartupPath + "/ezCPU(2).exe");
}
this.Text = this.Text + " - v" + Application.ProductVersion;
this.abVersion.Text = "Version: " + Application.ProductVersion;
//Call CPU class and display the results
cpu.GetCPUInfo();
DisplayCPUStats();
//Call the GPU class and display the results
gpu.GetGPUInfo();
DisplayGPUStats();
//Call the motherboard class and display the results
mb.GetMBInfo();
DisplayMBStats();
//Call the memory class and display the results
mem.GetRAMInfo();
DisplayMemoryStats();
}
//Pulls the information from the CPU class to display it on the form
public void DisplayCPUStats()
{
txtCPUName.Text = cpu.cpuName;
if (cpu.cpuManufacturer.Contains("Intel"))
{
txtCPUManufacturer.Text = "Genuine Intel";
}
else
{
txtCPUManufacturer.Text = cpu.cpuManufacturer;
}
txtCores.Text = cpu.cpuCores;
txtThreads.Text = cpu.cpuThreads;
txtMaxSpeed.Text = cpu.ConvertClockSpeed(cpu.cpuMaxSpeed);
txtCurrentSpeed.Text = cpu.ConvertClockSpeed(cpu.cpuCurrentSpeed);
txtCaption.Text = cpu.cpuCaption;
txtStatus.Text = cpu.cpuStatus;
txtArchitecture.Text = cpu.GetArchitecture(Convert.ToInt16(cpu.cpuArchitecture));
}
//Pulls the information from the GPU class to display it on the form
public void DisplayGPUStats()
{
txtGPUName.Text = gpu.gpuName;
txtGPUManufacturer.Text = gpu.gpuManufacturer;
txtGPUVideoMode.Text = gpu.gpuVideoMode;
txtGPURefresh.Text = gpu.gpuRefreshRate + " hertz";
txtGPUStatus.Text = gpu.gpuStatus;
txtGPUDriverVersion.Text = gpu.gpuDriverVersion;
txtGPUDriverDate.Text = gpu.gpuDriverDate;
}
//Pulls the information from the Motherboard class to display it on the form
public void DisplayMBStats()
{
//Motherboard information
txtMBManufacturer.Text = mb.mbManufacturer;
txtMBModel.Text = mb.mbModel;
txtMBSerial.Text = mb.mbSerial;
txtMBBusType.Text = mb.mbBusType;
txtMBStatus.Text = mb.mbStatus;
//BIOS information
txtBIOSVersion.Text = mb.biosVersion;
txtBIOSDate.Text = mb.biosDate;
txtBIOSBrand.Text = mb.biosManufacturer;
}
//Pulls the information from the Memory class to display it on the form
public void DisplayMemoryStats()
{
//RAM information
txtRAMSize.Text = mem.BytesToGB(mem.ramSize);
txtRAMManufacturer.Text = mem.ramManufacturer;
txtRAMType.Text = mem.GetRAMType(Convert.ToInt16(mem.ramType));
txtRAMFrequency.Text = String.Format("{0:n1}", Convert.ToInt16(mem.ramFrequency)) + " MHz";
}
//Visit the GitHub repo on click
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("https://github.com/ioschris/ezCPU");
}
//Check for update and download if it exists
public void DownloadUpdate()
{
//URL of the updated file
string url = "http://www.chrisharrisdev.com/ezcpu/ezCPU.exe";
//Declare new WebClient object
WebClient wc = new WebClient();
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadFileAsync(new Uri(url), Application.StartupPath + "/ezCPU(1).exe");
}
//When the download completes, show the message box and restart the application
void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
//Show a message when the download has completed
MessageBox.Show("ezCPU is now up-to-date!\n\nThe application will now restart!", "ezCPU - Update Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Restart();
}
//Create method to check for an update
public void GetUpdate()
{
//Declare new WebClient object
WebClient wc = new WebClient();
string textFile = wc.DownloadString("http://www.chrisharrisdev.com/ezcpu/ezcpu_version.txt");
newVersion = textFile;
//If there is a new version, call the DownloadUpdate method
if (newVersion != Application.ProductVersion)
{
MessageBox.Show("An update is available!\n\nClick OK to download and restart!", "ezCPU - Update Available", MessageBoxButtons.OK, MessageBoxIcon.Information);
DownloadUpdate();
}
else
{
MessageBox.Show("ezCPU is up-to-date!\n\nThere is not an update that needs to be applied!", "ezCPU - Up-to-Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//When the app restarts, rename the updated file, rename the original file, and delete the old version
private void ezCPU_FormClosed(object sender, FormClosedEventArgs e)
{
//This renames the original file so any shortcut works and names it accordingly after the update
if (System.IO.File.Exists(Application.StartupPath + "/ezCPU(1).exe"))
{
System.IO.File.Move(Application.StartupPath + "/ezCPU.exe", Application.StartupPath + "/ezCPU(2).exe");
System.IO.File.Move(Application.StartupPath + "/ezCPU(1).exe", Application.StartupPath + "/ezCPU.exe");
}
}
//Check for updates
private void button1_Click(object sender, EventArgs e)
{
GetUpdate();
}
}
}
Upvotes: 0
Reputation: 2568
Your problem might be that executable is still running. Since you can't delete it when OnLoad, your previous process might be not closed yet. I'll suggest a workaround here. Kill any other duplicate process before you delete it.
Process process = Process.GetCurrentProcess();
var dupl = ( Process.GetProcessesByName( process.ProcessName ) );
if( dupl.Length <= 1 ) {
return true;
}
foreach( var p in dupl ) {
if( p.Id == process.Id ) {
continue;
}
p.Kill();
}
Edit: I'll suggest you to find the root cause why your old process doesn't closed properly when it gets closed.
Upvotes: 1