[英]Access to the path "PATH" is denied - File.Delete
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错误: 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.
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.
由于您无法在 OnLoad 时删除它,因此您之前的进程可能尚未关闭。 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.编辑:我建议您找出旧进程关闭时未正确关闭的根本原因。
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.