0

is there a way i can get all included class in solution ? atleast allClassNames e.g classname.cs

but the thing was create the code for finding includedClass in other solution . Means not the solution where I'm doing code but the other solution .

Here's what ive researched so far:

Parsing Visual Studio Solution files

but im still confuse on how to get it via path (of .sln)? or how can i implement it . thanks in advance!

Community
  • 1
  • 1
Elegiac
  • 361
  • 2
  • 15
  • 1
    You talk about determining the included classes, but it sounds like what you actually want is the included C# source files. There is no rule that says that the name of the source file must match the name of the class (although that is a good rule of thumb), and in fact one source file may contain many classes, and one class may be implemented in several source files. – RenniePet Nov 28 '14 at 21:22

1 Answers1

0

What you can do is first read the solutiin file and get the included projects If you open the solution file using notepad you ill notice that the project listed as the folow

Project("{00000000-0000-0000-0000-000000000000}") = "project name", "project path", "{00000000-0000-0000-0000-000000000000}"

So for example you can use reguler expretion to get a list of the projects after you get the list of the projects you can read the project file and ge a list of all code files the project file is in xml format

        string solutionFile="the solution file";FileInfo fInfo = new FileInfo(solutionFile);
        string solutionText = File.ReadAllText(solutionFile);
        Regex reg = new Regex("Project\\(\"[^\"]+\"\\) = \"[^\"]+\", \"([^\"]+)\", \"[^\"]+\"");
        MatchCollection mc = reg.Matches(solutionText);
        List<string> files = new List<string>();
        foreach (Match m in mc)
        {
            string project_file = m.Groups[1].Value;
            project_file = System.IO.Path.Combine(fInfo.Directory.FullName, project_file);
            if (System.IO.File.Exists(project_file))
            {
                string project_path = new FileInfo(project_file).DirectoryName;
                XmlDocument doc = new XmlDocument();
                doc.Load(project_file);                    
                XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
                ns.AddNamespace("ms", "http://schemas.microsoft.com/developer/msbuild/2003");
                System.Xml.XmlNodeList list = doc.ChildNodes[1].SelectNodes("//ms:ItemGroup/ms:Compile", ns);
                foreach (XmlNode node in list)
                {
                    files.Add(Path.Combine(project_path, node.Attributes["Include"].InnerText));
                }
            }
        }

Update for references: you can use this code to read the reference.

               XmlNodeList references = doc.ChildNodes[1].SelectNodes("//ms:ItemGroup/ms:Reference", ns);
                foreach (XmlNode node in references)
                {
                    string name_space = node.Attributes["Include"].InnerText;
                    string name_space_path;
                    XmlNode nHintPath = node.SelectSingleNode("//ms:HintPath", ns);
                    if (nHintPath != null)
                    {
                        name_space_path = nHintPath.InnerText;
                        if (!Path.IsPathRooted(name_space_path))
                        {
                            name_space_path = Path.Combine(project_path, name_space_path);
                        }
                    }
                }
Aladin Hdabe
  • 865
  • 6
  • 6