Deleting an exe in code gives "Access To Path c:\'Myfolder\myfile.exe" is denied

I have an application that is supposed to autoupdated itself when it sees a file in a certain directory at the start of running the application.

The application, in order to update itself, runs a very small utility application that simply closes the original application, waits 10 seconds to ensure it is properly closed, deletes the original application exe file and then moves the replacement into place from a local folder, renaming properly as it does so, runs the updated application and then closes itself.  

The application and updater as well as the files for update are set to not propagate permissions of the parent fiolder and full permission is given to everyone on the network.

Further testing has revealed that the code is failing on the delete line.

Who is Participating?
sounds like application number two is keeping teh context of application one open.  Are you sure that application number one is properly shut down?  do you store the state of the main application in some file, que, etc. that says that it is shut down as per the request of application number 2?  this way you can insure that your application is properly closed and you can ditch the time and pray approach.
Why don't you rename instead of delete? Then next time you run the updater, delete the old file so you can rename again?

If File.Exists(MyOldFile) Then File.Delete(MyOldFile)



Well, after reading your question again, that might not work. I went on the assumption that something from MyCurrentFile was hanging around in memory. But it's worth a try.

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

anwar ul haqueCommented:
You can not delete the application exe untill it is in running state or some other application is accessing it, so make sure that there is no other application is currently accessing it and it is properly shut down, recommened is to write your code in closing event. Delete the application exe from within external exe after some time...add delay and check if file exists then delete it..Hope it will help you

Anwar ul Haque
lgropperAuthor Commented:
Thanks, it turns out that I need to put a thread.sleep between the shutdown and the delete so that the delete didn't occur until all threads had been dropped from Application One... thanks a lot!
Roger that
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.