Search code examples
c#cefsharp

Save browser data in CefSharp


I have an Windows Form Application which utilises CefSharp. A while ago I used a chunk of code that saves the state of the browser such as login details etc so that the user does not have to log in multiple times.

This is the code I used:

    private static bool _hasRun;

        CefSettings settings = new CefSettings();
        if (!_hasRun)
        {
            Cef.Initialize(new CefSettings { CachePath = "MyCachePath", PersistSessionCookies = true });
        }
        _hasRun = true;
        string cache_dir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\CEF";
        settings.CachePath = cache_dir;
        settings.CefCommandLineArgs.Add("persist_session_cookies", "1");
        string link = Domain;
        chrome = new ChromiumWebBrowser(link);
        this.tabPage2.Controls.Add(chrome);
        chrome.Dock = DockStyle.Fill;

This code runs fine in my old application but when using this on my most recent app I receive this error message:

enter image description here

Any suggestions?


Solution

  • You must provide a full path.

    non-absolute: "MyCachepath"

    absolute: "C:\users\username\documents\MyCachepath"

    why did it work in older projects?

    Some project types and versions automaticallly translate an relative path into an absolute path like so:

    environment.CurrentDirectory + @"\MyCachepath"