Skip to content

Instantly share code, notes, and snippets.

@cairey
Last active August 20, 2021 12:07
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cairey/5501024 to your computer and use it in GitHub Desktop.
Save cairey/5501024 to your computer and use it in GitHub Desktop.
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
Copy link

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