无法加载文件或程序集"path"或其依赖项之一。系统找不到指定的文件



我有一项服务。我试着用c#安装它。即使存在服务文件,我也收到错误消息Could not load file or assembly 'file:///C:Samplesample.exe' or one of its dependencies. The system cannot find the file specified.我使用installutil进行安装,它成功了。

string Path = @"C:samplesample.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);
}

该代码产生错误installutil成功的路径相同。我检查了路径,sample.exe文件存在于指定的位置。为什么会出现这种错误?

编辑

第一次运行时,此代码文件不存在,将发生异常。届时,我将把文件复制到指定的位置,并再次调用相同的代码。在第二次实际文件存在,但显示相同的错误消息。

使用AssemblyInstaller后,我们需要卸载文件。

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);

使用AppDomain,我们可以卸载程序集。通过此.exe文件将在操作后发布

相关内容

最新更新