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:

C#

Browser browser = BrowserFactory.Create();

VB.NET

Dim browser As Browser = BrowserFactory.Create()

The code above equals to:

C#

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

VB.NET

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:

C#

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

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

VB.NET

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:

C#

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

VB.NET

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.