2008-11-30 50 views

回答

2

下面是一些代码:

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; 
} 

要使用它,你需要发现路径web项目的测试下的csproj文件。我会把它作为读者的练习(我目前已经将其编码为硬编码)。

8

我刚刚发布了CassiniDev 3.5.1/4.0.1测试版,如果您有兴趣,请使用简单的测试夹具示例。

卡西尼用于开发和测试人员:http://cassinidev.codeplex.com

莫斗鱼,文字。

相关问题