Reputation: 416
I have an update button within the windows form application.When the user clicks on the update button, the application checks the current version of the application with the version available in the server obtained from the webservice. If there is mismatch among versions,the application will download the new version from the path obtained from the webservice.
I am currently using two projects within the same solution
Main project where the application is running
Update project -Its purpose is used to delete the .exe file and download the new .exe file. (Update project is added as a reference of Main project)
The problem is when i try to delete the mainproject.exe through code in update project,it shows an exception saying "Unauthorized exception caught". Does anyone knows why this is happening?OR Does anyone have a better idea to use update function within the application??
This is the code that i am using for the deleting the file.
Unauthorized Exception in Windows Forms - C#
Edit:-
While i was debugging the application,iam able to delete the .exe file.But when i try to delete the application after installing in the desktop,again iam getting the exception message as "Access is denied".
Upvotes: 2
Views: 2171
Reputation: 416
I found the solution why iam getting the "access is denied" exception in my application.
Since iam deleting a file inside the application through code i need to have the privilege of "Administrator".
One way is to make the user login manually as administrator.But that is not a better option.
Another way is to create an App Manifest file within your project and set the level as "administartor."
Creating App Manifest--> Right click on the project->Add new item-->Select App Manifest option from the right pane->Click ok
Open the manifest file and change the level to "requireAdministartor".
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
This will solve the issue while running the application,it will prompt user to run as administrator.
Hope this will be helpful to someone in future. :) Thank you guys for your support.
Upvotes: 3
Reputation: 11494
In you update button, you start another small app as a separate process , in the small app, you can use the following code to kill your process, and then delete the original app.
try
{
Process [] proc Process.GetProcessesByName("YourAppName");
proc[0].Kill();
}
Upvotes: 4
Reputation: 7017
Is the application running and that's why it is unable to delete the executable? If so, you could rename the running executable and put the new version in its place. The new version will then be executed the next time the application is started.
Upvotes: 2