这是整体代码。第一种方法读取 .sln 文件并使用正则表达式识别 .csproj 文件。第二种方法采用 .csproj 文件的路径并对其进行解析以找出引用的 dll 位置(请注意,它不会识别 GAC 安装的程序集) -
public void GetAllCSProjectFiles()
{
var Content = File.ReadAllText("PathTo.sln");
Regex projReg = new Regex(
"Project\\(\"\\{[\\w-]*\\}\"\\) = \"([\\w _]*.*)\", \"(.*\\.(cs|vcx|vb)proj)\""
, RegexOptions.Compiled);
var matches = projReg.Matches(Content).Cast<Match>();
var Projects = matches.Select(x => x.Groups[2].Value).ToList();
for (int i = 0; i < Projects.Count; ++i)
{
if (!Path.IsPathRooted(Projects[i]))
Projects[i] = Path.Combine(Path.GetDirectoryName("PathTo.sln"),
Projects[i]);
Projects[i] = Path.GetFullPath(Projects[i]);
CheckForDllReferences(Projects[i]);
}
}
public static void CheckForDllReferences(String csprojFile)
{
XmlDocument xdDoc = new XmlDocument();
xdDoc.Load(csprojFile);
XmlNamespaceManager xnManager =
new XmlNamespaceManager(xdDoc.NameTable);
xnManager.AddNamespace("tu",
"http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode xnRoot = xdDoc.DocumentElement;
XmlNodeList xnlPages = xnRoot.SelectNodes("//tu:HintPath", xnManager);
foreach (XmlNode node in xnlPages)
{
string location = node.InnerText.ToLower();
//do something
}
}