Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.

Each Browser instance must be created using BrowserFactory. BrowserFactory initializes the context that is needed for a browsing session and provides the path to the directory where Chromium stores cookies, cache data files, etc.

The following code creates a new Browser instance initialized with default context:


Browser browser = BrowserFactory.Create();


Dim browser As Browser = BrowserFactory.Create()

The code above equals to:


Browser browser = BrowserFactory.Create(BrowserContext.DefaultContext);


Dim browser As Browser = BrowserFactory.Create(BrowserContext.DefaultContext)

The BrowserContext.DefaultContext property is a default BrowserContext that is configured to store Chromium data files such as cookies, cache, etc. in user's temp directory. You can get absolute path to the directory where the data files are stored using the BrowserPreferences.GetDefaultDataDir() method.

Two Browser instances with same BrowserContext will use the same user data directory. As result, they will share cookies and cache files. For example:


BrowserContextParams params= new BrowserContextParams("C:\\my-data1");
BrowserContext context = new BrowserContext(params);

Browser browser1 = BrowserFactory.Create(context);
Browser browser2 = BrowserFactory.Create(context);


Dim params As BrowserContextParams = New BrowserContextParams("C:\\my-data1")
Dim context As BrowserContext = New BrowserContext(params)

Dim browser1 As Browser = BrowserFactory.Create(context)
Dim browser2 As Browser = BrowserFactory.Create(context)

To create independent Browser instances that don't share cookies and cache data, you must initialize each Browser instance with a different BrowserContext configured to use different data directory. For example:


BrowserContextParams params1 = new BrowserContextParams("C:\\my-data1");
BrowserContext context1 = new BrowserContext(params1);
Browser browser1 = BrowserFactory.Create(context1);

BrowserContextParams params2= new BrowserContextParams("C:\\my-data2");
BrowserContext context2 = new BrowserContext(params2);
Browser browser2 = BrowserFactory.Create(context2);


Dim params1 As BrowserContextParams = New BrowserContextParams("C:\\my-data1")
Dim context1 As BrowserContext = New BrowserContext(params1)
Dim browser1 As Browser = BrowserFactory.Create(context1)

Dim params2 As BrowserContextParams = New BrowserContextParams("C:\\my-data2")
Dim context2 As BrowserContext = New BrowserContext(params2)
Dim browser2 As Browser = BrowserFactory.Create(context2)

To get BrowserContext of the Browser instance you can use the browser.Context property.

Important: please make sure that you don't use several BrowserContext instances configured to use same data directory.  In this case Chromium engine will read/write same data directory from different Browser instances which might cause unexpected behavior or errors including a crash in the Chromium engine.