Kumar Vaibhav
Kumar Vaibhav

Reputation: 2642

List all referenced (don't think loaded) assemblies for a given .net solution

I need to create a unit test to enumerate the Locations of all the referenced assemblies for a given .net solution. I have been searching and have not been able to find anything that suits the problem.

I took a look at Is there a way to force all referenced assemblies to be loaded into the app domain? and Get the paths of all referenced assemblies

The GetAssemblies() method gives us the assemblies that have been loaded. But I also want the paths of those assemblies that have not been loaded.

Simply speaking, I just want to know the locations of all the assemblies that I see when expand the 'References' of the various projects in Visual Studio.

If I look at the .csproj files then I can see all the references. Could there be any build scripty solution to this? Just wondering.

Ideas?

Thanking in anticipation.

Upvotes: 1

Views: 5380

Answers (2)

Kumar Vaibhav
Kumar Vaibhav

Reputation: 2642

Here's the overall code. The first method reads the .sln file and identifies .csproj files using regex. The second method takes in the path of the .csproj file and parses it to find out referenced dll locations (note that it does not identifies GAC installed assemblies) -

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

Upvotes: 1

Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

I believe you wants to get the list of all references of dlls in complete solution. so first get the list of all projects of current solution:

How do I programmatically list all projects in a solution?

Now will have the list of all projects of the solution, now you can read .csproj files (of every project) and read the tag under ItemGroup tag.

as if you will try to get it using any inbuilt function then it will only give you list of those assemblies which got loaded.. but will not give list of assemblies which are missing (but referenced).

Some other usefull similar links:

Resolve assembly references from another folder

http://www.codeproject.com/Questions/88183/How-to-get-all-dll-name-which-is-reference-in-C-pr

Upvotes: 1

Related Questions