Search code examples
c#windowsservice.net-assemblyassemblyinfo

Could not load file or assembly "path" or one of its dependencies. The system cannot find the file specified


I have a service. I tried to install it using c#. Even though service file is present i got error message Could not load file or assembly 'file:///C:\Sample\sample.exe' or one of its dependencies. The system cannot find the file specified. i used installutil for installation and it succeeded.

           string Path = @"C:\sample\sample.exe";
           string[] commandLineOptions = new string[1] { "/LogFile=install.log" };
           using (AssemblyInstaller installer = new AssemblyInstaller(Path, commandLineOptions))
            {
                installer.UseNewContext = true;
                installer.Install(null);
                installer.Commit(null);
            }

this code produced error same path with installutil succeede. I checked path, sample.exe file was present in the specified location. why this error occurs?

Edit

first time while run this code file is not present and exception will occure. at that time i will copy file to the specified location and same code call again. in second time actually file exists but same error message is showing.


Solution

  • after using AssemblyInstaller we need to unload the files.

    var domain = AppDomain.CreateDomain("MyDomain");
                    using (AssemblyInstaller installer = domain.CreateInstance(typeof(AssemblyInstaller).Assembly.FullName, typeof(AssemblyInstaller).FullName, false, BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.ExactBinding, null, new Object[] { Path, new String[] { } }, null, null, null).Unwrap() as AssemblyInstaller)
                    {
                        installer.UseNewContext = true;
                        installer.Install(null);
                        installer.Commit(null);
                    }
                    AppDomain.Unload(domain);
    

    using a AppDomain we can unload the assembly. By this .exe file will be released after the operation