2016-12-01 144 views
1

我有使用CefSharp与初始化设置缓存路径简单的WPF应用程序:C#CefSharp WPF着定位依赖条件

try 
{ 
    var settings = new CefSettings(); 
    settings.CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Cache"); 
    Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null); 
} 
catch (Exception e) 
{ 
    MessageBox.Show(e.ToString()); 
} 

它运作良好,但我使用此代码,以便启动时,系统启动:

Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
key.SetValue(Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().Location); 

如果应用程序是由系统启动时启动我得到这个错误在catch块:

System.Exception: Unable to lacte requred Cef/CefSharp dependencies: 
Missing:CefSharp.BrowserSubprocess.exe 
Missing:CefSharp.BrowserSubProcess.Core.dll 
Missing:CefSharp.Core.dll 
Missing:icudtl.dat 
Missing:libcef.dll 

但是,当我运行应用程序manualy它效果很好。 谢谢!

+0

这是因为您尚未在注册表中设置启动密钥的工作目录。 –

+0

@AliBahraminezhad你能帮我吗? - 我可以在哪里设置工作目录? – Fori

+1

所以我找到解决方案:'Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);' - 添加berfore初始化。谢谢@AliBahraminezhad – Fori

回答

0

这是因为working directory不是您的应用程序的bin目录。

Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
key.SetValue(Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().Location); 

有很多方法可以将当前目录更改为您的应用程序的bin路径。最简单的方法是在您的代码中更改当前目录(如您所建议的那样)。

Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain‌​.BaseDirectory);