Note: Advice in this article will only work for DotNetBrowser 1.
See the corresponding article for DotNetBrowser 2 here.
Up to version 1.11.1:
The user-agent string can be modified only once, before you create any Browser
instances. You can provide your own user-agent string using the BrowserPreferences.SetUserAgent(String userAgent)
method:
C#
BrowserPreferences.SetUserAgent("My User Agent String");
VB.NET
BrowserPreferences.SetUserAgent("My User Agent String")
From version 1.12:
The user-agent string can be modified during runtime. You can set your own user-agent string or get it using the Browser.UserAgent
property:
C#
Browser browser = BrowserFactory.Create(); browser.UserAgent = "Modified User Agent String"; string currentUserAgentString = browser.UserAgent;
VB.NET
Dim browser As Browser = BrowserFactory.Create() browser.UserAgent = "Modified User Agent String" Dim currentUserAgentString As String = browser.UserAgent
Example
MainWindow.xaml
<Window x:Class="WPF.UserAgent.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid Name="mainLayout"> </Grid> </Window>
C#
MainWindow.xaml.cs
using System; using System.IO; using DotNetBrowser; using DotNetBrowser.WPF; using System.Windows; namespace WPF.UserAgent { public partial class MainWindow : Window { Browser browser; WPFBrowserView browserView; public MainWindow() { BrowserPreferences.SetUserAgent("My User Agent String"); InitializeComponent(); browser = BrowserFactory.Create(); browserView = new WPFBrowserView(browser); mainLayout.Children.Add(browserView); browser.UserAgent = "Modified User Agent String"; browserView.Browser.LoadURL("http://whatsmyuseragent.com/"); } } }
VB.NET
MainWindow.xaml.vb
Imports DotNetBrowser Imports DotNetBrowser.WPF Namespace WPF.UserAgent Class MainWindow Dim browser As Browser Dim browserView As WPFBrowserView Public Sub New() BrowserPreferences.SetUserAgent("My User Agent String") InitializeComponent() browser = BrowserFactory.Create() browserView = New WPFBrowserView(browser) mainLayout.Children.Add(browserView) browser.UserAgent = "Modified User Agent String" browserView.Browser.LoadURL("http://whatsmyuseragent.com/") End Sub End Class End Namespace