Click here to Skip to main content
15,124,134 members
Articles / Programming Languages / C#
Posted 21 Mar 2015

Tagged as


5 bookmarked

Starting an InPrivate instance of Internet Explorer in CodedUI

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
21 Mar 2015CPOL2 min read
How to start an InPrivate instance of Internet Explorer by using BrowserWindow object.


During the preparation of one of my CodedUI tests I came in situation in which I needed to start Internet Explorer with InPrivate Browsing set to active. There is no such a property on BrowserWindow object that can be set, probably because this is a specific Internet Explorer option (although present as functionality with a different name on other browsers).

So how do we do it?


A quick search on Google, surprisingly, didn't gave any results. So I dug into this small challenge. In order to start Internet Explorer with InPrivate Browsing set to active it is necessary to pass the -private argument to the call of the executable. So how to pass an argument to the BrowserWindow instance that we are creating?

Using the code

It's not obvious as not often used but, BrowserWindow.Launch static method has, aside the usual signature accepting an Uri object as a parameter, an overload that accepts a variable number of string  arguments (params string[]). The string arguments specified in the call of this method, will be passed as arguments to the process that is going to be started by invoking the BrowserWindow.Launch, in this case to the process of Internet Explorer. This is quite handy as we can pass our URL and and the necessary -private argument to Internet Explorer and achieve the desired result.

BrowserWindow.Launch("", "-private");

How does it work?

If you peek inside the BrowserWindow class, you will see that underneath it is starting a process and it passes all of the parameters as an array of string to the Arguments property.

Process process = new Process {StartInfo = {FileName = "iexplore.exe"}};
StringBuilder commandLine = new StringBuilder();
    str => commandLine.AppendFormat(
        "{0} ", 
        new object[] { str }));
process.StartInfo.Arguments = commandLine.ToString();
The same can be achieved also by using the following approach:
Process ieInPrivate = new Process();
ieInPrivate.StartInfo.FileName = "iexplore";
ieInPrivate.StartInfo.Arguments = " -private";
ieInPrivate.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;

BrowserWindow browser = BrowserWindow.FromProcess(ieInPrivate);

At the end the result will be a successfully launched Internet Explorer with InPrivate Browsing set to active.

Image 1

There are also some other handy option that you can pass to Internet Explorer like -extoff that will start Internet Explorer in No Add-ons mode. For a complete list of options check the following page on MSDN.

Points of Interest

By exploring this kind of tasks makes you peak inside the code of the objects you are using on daily basis. It will help you to understand how all of it works and put some valuable knowledge in your toolbox. It can be very helpful in the future.

I encourage you to check these libraries with dotPeek or a .NET Decompiler of your choice in order to understand better how they relate and achieve this functionality. Classes as BrowserWindow, IEBrowserService, InternetExplorerWrapper, ShDocVw and how the relate is quite important to fully understand CodedUI.

Happy coding!


Initial release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mario Majčica
Software Developer (Senior)
Netherlands Netherlands
An accomplished software engineer specialized in object-oriented design and analysis on Microsoft .NET platform with extensive experience in the full life cycle of the software design process.
Experienced in agile software development via scrum and kanban frameworks supported by the TFS ALM environment and JIRA. In depth know how on all automation process leading to continuous integration, deployment and feedback.
Additionally, I have a strong hands-on experience on deploying and administering Microsoft Team Foundation Server (migrations, builds, deployment, branching strategies, etc.).

Comments and Discussions

QuestionWhere is browserwindow namespace? Pin
MrDeej31-Mar-15 2:55
MemberMrDeej31-Mar-15 2:55 
AnswerRe: Where is browserwindow namespace? Pin
Mario Majčica31-Mar-15 4:21
professionalMario Majčica31-Mar-15 4:21 
Dear MrDeej,

this should be quite an easy thing to achieve. BrowserWindow class is part of Microsoft.VisualStudio.TestTools.UITesting namespace, which is situated in Microsoft.VisualStudio.TestTools.UITesting.dll assembly. Usually this assembly is located in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PublicAssemblies\ and should be registered in GAC.

The easiest way to get a project with all the necessary is to create a CodedUI project. In that way all the necessary references should be in place.

Also if you are calling it out of the class that has no CodedUITestAttribute applied make sure that you initiate the Playback correctly.

I hope this answers your question.


“The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser men so full of doubts.”

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.