2016-12-01 145 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);