Вот код:
private static void GetDevelopmentServerVPathAndPortFromProjectFile(
string csprojFileName,
out string developmentServerVPath,
out int developmentServerPort)
{
XPathDocument doc = new XPathDocument(csprojFileName);
XPathNavigator navigator = doc.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("msbuild",
"http://schemas.microsoft.com/developer/msbuild/2003");
const string xpath = "/msbuild:Project/msbuild:ProjectExtensions/"
+ "msbuild:VisualStudio/msbuild:FlavorProperties/"
+ "msbuild:WebProjectProperties";
XPathNavigator webProjectPropertiesNode =
navigator.SelectSingleNode(xpath, manager);
XPathNavigator developmentServerPortNode =
webProjectPropertiesNode.SelectSingleNode("msbuild:DevelopmentServerPort",
manager);
XPathNavigator developmentServerVPathNode =
webProjectPropertiesNode.SelectSingleNode("msbuild:DevelopmentServerVPath",
manager);
developmentServerPort = developmentServerPortNode.ValueAsInt;
developmentServerVPath = developmentServerVPathNode.Value;
}
private static string GetCommonProgramFilesPath()
{
string commonProgramFiles =
Environment.GetEnvironmentVariable("CommonProgramFiles(x86)");
if (string.IsNullOrEmpty(commonProgramFiles))
{
commonProgramFiles =
Environment.GetEnvironmentVariable("CommonProgramFiles");
}
if (string.IsNullOrEmpty(commonProgramFiles))
{
commonProgramFiles =
Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
}
return commonProgramFiles;
}
private static Process PrepareCassiniProcess(int developmentServerPort,
string projectPhysicalPath,
string developmentServerVPath)
{
string commonProgramFiles = GetCommonProgramFilesPath();
string cassiniPath = Path.Combine(commonProgramFiles,
@"Microsoft Shared\DevServer\9.0\WebDev.WebServer.exe");
string cassiniArgs = string.Format(
CultureInfo.InvariantCulture,
"/port:{0} /nodirlist /path:\"{1}\" /vpath:\"{2}\"",
developmentServerPort, projectPhysicalPath, developmentServerVPath);
Process cassiniProcess = new Process();
cassiniProcess.StartInfo.FileName = cassiniPath;
cassiniProcess.StartInfo.Arguments = cassiniArgs;
return cassiniProcess;
}
Чтобы использовать его, вам необходимо найти путь к файлу CSPROJ тестируемого веб-проекта. Я оставлю это как упражнение для читателя (в настоящее время я получил его жестко).