Failed to delete .exe file via C #

I have a refresh button in a windows form application. When the user clicks the update button, the application checks the current version of the application with the version available on the server obtained from webservice. If there is a mismatch between the versions, the application will download the new version from the path obtained from the web service.

I am currently using two projects in one solution

  • The main project in which the application is running

  • Refresh project. Its purpose is to delete the .exe file and download the new .exe file. (The update project is added as a link to the main project)

The problem is that when I try to delete mainproject.exe through the code in the update project, it displays an exception that says: "An unauthorized exception has been fixed." Does anyone know why this is happening? OR Does anyone have a better idea to use the update feature in an application?

This is the code I use to delete a file.

Unauthorized Exception in Windows Forms - C #

Edit: -

While I was debugging the application, iam was able to delete the .exe file. But when I try to uninstall the application after installing it on the desktop, again I get the exception message as "Access is denied."

+2
source share
3 answers

I found a solution why iam gets the "access denied" exception in my application.

Since iam deletes the file inside the application through the code, I need to have the โ€œAdministratorโ€ privilege.

One way is to manually enter the user into the system as an administrator. But this is not the best option.

Another way is to create an application manifest file in your project and set the level as "administartor".

Creating an application manifest โ†’ Right-click on the project-> Add new item โ†’ Select the App Manifest option in the right pane-> Click ok

Open the manifest file and change the level to "requireAdministartor".

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

This will solve the problem during application startup, and prompt the user to run it as an administrator.

Hope this will be useful to someone in the future. :) Thank you guys for your support.

+2
source

In your refresh button, you launch another small application as a separate process, in a small application you can use the following code to kill your process and then delete the original application.

 try { Process [] proc Process.GetProcessesByName("YourAppName"); proc[0].Kill(); } 
+4
source

Is the application running and why can't it delete the executable? If so, you can rename the executable executable and put the new version in its place. The new version will be executed the next time the application is launched.

+2
source

Source: https://habr.com/ru/post/1209722/


All Articles