MEF C# Service - DLL Updating

Posted by connerb on Stack Overflow See other posts from Stack Overflow or by connerb
Published on 2014-06-10T22:54:58Z Indexed on 2014/06/12 3:26 UTC
Read the original article Hit count: 126

Currently, I have a C# service that runs off of many .dll's and has modules/plugins that it imports at startup. I would like to create an update system that basically stops the service, deletes any files it is told to delete (old versions), downloads new versions from a server, and starts the service. I believe I have coded this right except for the delete part, because as long as I am not overwriting anything, the file will download. If I try to overwrite something, it won't work, which is why I am trying to delete it before hand. However, when I do File.Delete() to the path that I want to do, it gives me access to the path is denied. Here is my code:

new Thread(new ThreadStart(() =>
            {
                ServiceController controller = new ServiceController("client");
                controller.Stop();
                controller.WaitForStatus(ServiceControllerStatus.Stopped);
                try
                {
                    if (um.FilesUpdated != null)
                    {
                        foreach (FilesUpdated file in um.FilesUpdated)
                        {
                            if (file.OldFile != null)
                            {
                                File.Delete(Path.Combine(Utility.AssemblyDirectory, file.OldFile));
                            }
                            if (file.NewFile != null)
                            {
                                wc.DownloadFile(cs.UpdateUrl + "/updates/client/" + file.NewFile, Path.Combine(Utility.AssemblyDirectory, file.NewFile));
                            }
                        }
                    }


                    if (um.ModulesUpdated != null)
                    {
                        foreach (ModulesUpdated module in um.ModulesUpdated)
                        {
                            if (module.OldModule != null)
                            {
                                File.Delete(Path.Combine(cs.ModulePath, module.OldModule));
                            }
                            if (module.NewModule != null)
                            {
                                wc.DownloadFile(cs.UpdateUrl + "/updates/client/modules/" + module.NewModule, Path.Combine(cs.ModulePath, module.NewModule));
                            }
                        }
                    }


                }
                catch (Exception ex)
                {
                    Logger.log(ex);
                }


                controller.Start();

            })).Start();

I believe it is because the files are in use, but I can't seem to unload them. I though stopping the service would work, but apparently not. I have also checked the files and they are not read-only (but the folder is, which is located in Program Files, however I couldn't seem to get it to not be read-only programmatically or manually). The service is also being run as an administrator (NT AUTHORITY\SYSTEM). I've read about unloading the AppDomain but AppDomain.Unload(AppDomain.CurrentDomain); returned an exception as well.

Not too sure even if this is a problem with MEF or my program just not having the correct permissions...I would assume that it's mainly because the file is in use.

© Stack Overflow or respective owner

Related posts about c#

Related posts about service