Last active
August 20, 2021 12:07
-
-
Save cairey/5501024 to your computer and use it in GitHub Desktop.
Programmatically start IIS Express from code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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.