Create a gist now

Instantly share code, notes, and snippets.

@cairey /gist:5501024
Last active Nov 17, 2017

What would you like to do?
Programmatically start IIS Express from code.
public static class IISExpress
{
private static readonly List<string> sites = new List<string>();
private static readonly List<string> paths = new List<string>();
public static void StartIISExpress(string site, int port = 7329)
{
if(!sites.Contains(site.ToLower()))
sites.Add(site.ToLower());
else return;
var index = Environment.CurrentDirectory.LastIndexOf("\\bin\\");
var projectDir = Environment.CurrentDirectory.Remove(index);
var solutionDir = System.IO.Directory.GetParent(projectDir);
var path = solutionDir + "\\" + site;
var arguments = new StringBuilder();
arguments.Append(@"/path:");
arguments.Append(path);
arguments.Append(@" /Port:" + port);
// arguments.Append(@"/site:" + site);
var process = Process.Start(new ProcessStartInfo()
{
FileName = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\IIS Express\\iisexpress.exe",
Arguments = arguments.ToString(),
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
});
Thread.Sleep(10000);
}
public static void StartIISExpressFromPath(string path, int port = 7329)
{
if(!paths.Contains(path.ToLower()))
paths.Add(path.ToLower());
else return;
var arguments = new StringBuilder();
arguments.Append(@"/path:" + path);
arguments.Append(@" /Port:" + port);
var process = Process.Start(new ProcessStartInfo()
{
FileName = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\IIS Express\\iisexpress.exe",
Arguments = arguments.ToString(),
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
});
Thread.Sleep(10000);
}
}

peterdew commented Nov 1, 2017

Thank you very much for this.
In VS 2017 it is usefull to configure a path to the applicationhost.config since this is managed on a solutionbase in the .vs directory.

arguments.Append(@" /config:" + solutionDir + @"\.vs\config\applicationhost.config");

And then start using the site-argument instead of a path and port will help too. This brings you to the same config as debug-sessions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment