Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ademuanthony/e9e5a2a21b867eea09338840b3f4efa6 to your computer and use it in GitHub Desktop.
Save ademuanthony/e9e5a2a21b867eea09338840b3f4efa6 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment