Customized Solution & Project Explorer in .NET Using C# and Windows Forms

.NET has been in action for the past so many years. But Project and Solution files are still not familiar to most programmers. The reason behind this is Visual Studio manages the project and solution files for us. But if the project or solution contents are large then it will take a long time just to load and show the contents of them in the Solution Explorer of VS.
 
Usually, we just need to know what the files, references, and other contents are present in a project or list of projects present in a solution. This application will allow us to see any projects or solutions contents as in the Solution Explorer view without loading the heavy process VS.NET. I designed this application using C# and Windows Forms in .NET.
 
Before explaining its design and functionality, I will explain a little bit about its features.
 
Features of this Application:
  • Loads any C# Project and allows exploration of its contents.
  • Loads any Solution and allows to see the list of the projects and its contents.
  • Built-in support for displaying XML data in both XML and Data (Grid) View.
  • Support to explore references (.dll) like an object browser in VS.
  • Fast loading compared to the Solution Explorer of VS.
  • Ability to maintain a list of recent files opened through this application. 
Steps to create
 
Create a new Windows Forms Project in C# and name it SolutionExplorer.
 
Then place the controls as shown in the following figure:
 
ImageNew1.jpg
 
I will explain the purpose of each control on the startup form (Form1).
 
I used a TreeView control and ImageList to display the selected project or solution contents with images on each node.
 
Then I added a TabControl, in one tab I placed a RichTextBox, and in another tab, a datagrid control. Later, add menuitems to MainMenu as shown in Figure. Add context Menu with the following items:
  1. Expand,
  2. Collapse,
  3. Full Expand,
  4. Grid View
And set the context menu property of the TreeView to this control.
 
Finally, add an OpenFileDialog and tooltip control.
 
By using a FileDialog control, we can select any C# project file or Solution file to analyze and display its contents in a TreeView.
 
A ContextMenu for a TreeView is used to expand, collapse and display XML in a Grid.
 
I will explain what I have done in a Click of the Browse menuitem:
  1. if(DialogResult.OK == dglopensoln.ShowDialog())  
  2. {  
  3.     string selectedprjfile = dglopensoln.FileName.ToString();  
  4.     txtsolnpath.Text = selectedprjfile;  
  5.     toolTip1.SetToolTip(txtsolnpath,selectedprjfile);  
  6.     MenuItem testitem = new MenuItem(selectedprjfile);  
  7.     testitem.Click += new EventHandler(testitem_Click);  
  8.     mnurecent.MenuItems.Add(testitem);  
  9.     obj1.Text = selectedprjfile;  
  10.     menuItem7_Click(sender,e);  

Using the above code, we can browse the selected project file (.csproj) or solution (.soln), and then we are creating a new menuitem with text as the selected file path and a handler for the click of that menuitem. Then, we are internally calling the click handler of the analyze menuitem.
 
Here, I will explain the logic to load the Solution and its contents into the treeview control. I almost followed a similar logic to load the project also.
  1. string prevselfilecontents = selfilecontents.Text;  
  2. try  
  3. {  
  4.     selfilecontents.Text = "";  
  5.     treeView1.Nodes.Clear();  
  6.     menuItem7.Enabled = false;  
  7.     TreeNode mainnode = null;  
  8.     string httpprjs = null;  
  9.     if(txtsolnpath.Text.EndsWith(".sln"))  
  10.     {  
  11.         string tmpsolndata = @"c:\tempsoln.xml";  
  12.         string solnname = Path.GetFileNameWithoutExtension(txtsolnpath.Text);  
  13.         mainnode = new TreeNode(solnname);  
  14.         treeView1.Nodes.Add(mainnode);  
  15.         if(Directory.Exists(@"C:\"+solnname))  
  16.         {  
  17.             Directory.Delete(@"C:\"+solnname,true);  
  18.         }  
  19.         Directory.CreateDirectory(@"C:\"+solnname);  
  20.         prjsdetailsinsoln = new Hashtable();  
  21.         if(!File.Exists(tmpsolndata))  
  22.         {  
  23.             File.Create(tmpsolndata);  
  24.         }  
  25.         string solncontents = "";  
  26.         StreamReader solnreader = new StreamReader(txtsolnpath.Text.Trim());  
  27.         while(solncontents != null)  
  28.         {  
  29.             solncontents = solnreader.ReadLine();  
  30.             if(solncontents != null)  
  31.             {          
  32.    
  33.                 try  
  34.                 {  
  35.                     if(solncontents.StartsWith("Project"))  
  36.                     {  
  37.                         string[] prjprops = solncontents.Split(',');  
  38.                         string prjdispname = prjprops[0].Substring(prjprops[0].LastIndexOf("=")+1).Replace("\"","").Trim();  
  39.                         string prjpath1   = prjprops[1].ToString().Replace("\"","").Trim();  
  40.                         prjsdetailsinsoln.Add(prjdispname,prjpath1);  
  41.                     }  
  42.                 }  
  43.                 catch(Exception ex)  
  44.                 {  
  45.                     MessageBox.Show(ex.Message);  
  46.                 }  
  47.             }  
  48.         }  
  49.         IDictionaryEnumerator enprj = prjsdetailsinsoln.GetEnumerator();  
  50.         while (enprj.MoveNext())  
  51.         {  
  52.             string strprjname = enprj.Key.ToString();  
  53.             if(enprj.Value.ToString().IndexOf("http://") == -1 && enprj.Value.ToString().EndsWith(".csproj"))  
  54.             {  
  55.                 string prjpath = txtsolnpath.Text.Substring(0,txtsolnpath.Text.LastIndexOf(@"\")+1)+enprj.Value.ToString();  
  56.                 if(File.Exists(prjpath))  
  57.                 {  
  58.                     StreamReader prjreader = new StreamReader(prjpath);  
  59.                     string prjcontents = prjreader.ReadToEnd();  
  60.                     prjreader.Close();  
  61.                     //Format Project File into XML file...  
  62.                     tmpxmlfileforsolndata = @"C:\"+solnname+"\\"+enprj.Key.ToString()+".xml";  
  63.                     if(prjcontents.Length != 0)  
  64.                     {  
  65.                         StreamWriter solntoxmlconverter = new StreamWriter(tmpxmlfileforsolndata,false);  
  66.                         solntoxmlconverter.Write("<?xml version=\"1.0\"?>");  
  67.                         solntoxmlconverter.Write(prjcontents);  
  68.                         solntoxmlconverter.Close();  
  69.                     }  
  70.                     else  
  71.                     {  
  72.                         MessageBox.Show("Selected Project File is empty...");  
  73.                         txtsolnpath.Text = "";  
  74.                     }  
  75.                     //General project's Details....  
  76.                     TreeNode rootnode = new TreeNode();  
  77.                     rootnode.Text = strprjname;  
  78.                     mainnode.Nodes.Add(rootnode);  
  79.                     //Project's references information...  
  80.                     TreeNode referencenode = new TreeNode();  
  81.                     referencenode.Text = "References";  
  82.                     rootnode.Nodes.Add(referencenode);  
  83.                     //aspx Files Included in the Project...  
  84.                     TreeNode includedaspxfilesnode = new TreeNode();  
  85.                     includedaspxfilesnode.Text = "ASPX Files";  
  86.                     rootnode.Nodes.Add(includedaspxfilesnode);  
  87.                     //aspx.cs Files Included in the Project...  
  88.                     TreeNode includedaspxcsfilesnode = new TreeNode();  
  89.                     includedaspxcsfilesnode.Text = "ASPX.CS Files";  
  90.                     rootnode.Nodes.Add(includedaspxcsfilesnode);  
  91.                     //Class  Files Included in the Project...  
  92.                     TreeNode includedcsfilenode = new TreeNode();  
  93.                     includedcsfilenode.Text = "Class Files";  
  94.                     rootnode.Nodes.Add(includedcsfilenode);  
  95.                     //Class  Files Included in the Project...  
  96.                     TreeNode includedusercntrlfilenode = new TreeNode();  
  97.                     includedusercntrlfilenode.Text = "User Controls";  
  98.                     rootnode.Nodes.Add(includedusercntrlfilenode);  
  99.                     //Web config Files Included in the Project...  
  100.                     TreeNode includedwebconfignode = new TreeNode();  
  101.                     includedwebconfignode.Text = "Web Config Files";  
  102.                     rootnode.Nodes.Add(includedwebconfignode);  
  103.                     //Javascript Files Included in the Project...  
  104.                     TreeNode includedjsfilenode = new TreeNode();  
  105.                     includedjsfilenode.Text = "JavaScript Files";  
  106.                     rootnode.Nodes.Add(includedjsfilenode);  
  107.                     //CSS  Files Included in the Project...  
  108.                     TreeNode includedcssfilenode = new TreeNode();  
  109.                     includedcssfilenode.Text = "CSS Files";  
  110.                     rootnode.Nodes.Add(includedcssfilenode);  
  111.                     //Image  Files Included in the Project...  
  112.                     TreeNode includedimgfilenode = new TreeNode();  
  113.                     includedimgfilenode.Text = "Image Files";  
  114.                     rootnode.Nodes.Add(includedimgfilenode);  
  115.                     //XML  Files Included in the Project...  
  116.                     TreeNode includedxmlfilenode = new TreeNode();  
  117.                     includedxmlfilenode.Text = "XML Files";  
  118.                     rootnode.Nodes.Add(includedxmlfilenode);  
  119.                     //XSL  Files Included in the Project...  
  120.                     TreeNode includedxslfilenode = new TreeNode();  
  121.                     includedxslfilenode.Text = "XSL Files";  
  122.                     rootnode.Nodes.Add(includedxslfilenode);  
  123.                     //Other  Files present in unknown format Included in the Project...  
  124.                     TreeNode includedunknownfilenode = new TreeNode();  
  125.                     includedunknownfilenode.Text = "Unknown Files";  
  126.                     rootnode.Nodes.Add(includedunknownfilenode);  
  127.                     //To Load all items of the Project into the Treeview...  
  128.                     XPathDocument prjfile = new XPathDocument(tmpxmlfileforsolndata);  
  129.                     XPathNavigator nav = prjfile.CreateNavigator();  
  130.                     //TO Load all references of the selected Project...  
  131.                     XPathNodeIterator referenceiterator = nav.Select  
  132.                     (@"/VisualStudioProject/CSHARP/Build/References/Reference");  
  133.                     while(referenceiterator.MoveNext())  
  134.                     {  
  135.                         string reffile = referenceiterator.Current.GetAttribute("Name","").ToString();  
  136.                         TreeNode refnode = new TreeNode(reffile);  
  137.                         if(reffile.StartsWith("System"))  
  138.                         {  
  139.                             refnode.ForeColor  = Color.Green;  
  140.                         }  
  141.                         else  
  142.                         {  
  143.                             refnode.ForeColor = Color.Orange;  
  144.                         }  
  145.                         referencenode.Nodes.Add(refnode);  
  146.                     }  
  147.                     //TO Load all ASPX of the selected Project...  
  148.                     XPathNodeIterator aspxfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  149.                     LoadselectedDetails("aspx",includedaspxfilesnode,aspxfileiterator);  
  150.                     RemoveUnwatedNodes(includedaspxfilesnode);  
  151.                     //TO Load all ASPX.CS(Code-Behind) of the selected Project...  
  152.                     XPathNodeIterator aspxcsfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  153.                     LoadselectedDetails("aspx.cs",includedaspxcsfilesnode,aspxcsfileiterator);  
  154.                     RemoveUnwatedNodes(includedaspxcsfilesnode);  
  155.                     //TO Load all Assembly Config of the selected Project...  
  156.                     XPathNodeIterator webconfigfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  157.                     LoadselectedDetails("config",includedwebconfignode,webconfigfileiterator);  
  158.                     RemoveUnwatedNodes(includedwebconfignode);  
  159.                     //TO Load all Assembly Config of the selected Project...  
  160.                     XPathNodeIterator jsfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  161.                     LoadselectedDetails("js",includedjsfilenode,jsfileiterator);  
  162.                     RemoveUnwatedNodes(includedjsfilenode);  
  163.                     //TO Load all CSS files of the selected Project...  
  164.                     XPathNodeIterator cssfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  165.                     LoadselectedDetails("css",includedcssfilenode,cssfileiterator);  
  166.                     RemoveUnwatedNodes(includedcssfilenode);  
  167.                     //TO Load all Image files of the selected Project...  
  168.                     XPathNodeIterator imgfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  169.                     LoadselectedDetails("gif",includedimgfilenode,imgfileiterator);  
  170.                     RemoveUnwatedNodes(includedimgfilenode);  
  171.                     //TO Load all XML files of the selected Project...  
  172.                     XPathNodeIterator xmlfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  173.                     LoadselectedDetails("xml",includedxmlfilenode,xmlfileiterator);  
  174.                     RemoveUnwatedNodes(includedxmlfilenode);  
  175.                     //TO Load all XSL files of the selected Project...  
  176.                     XPathNodeIterator xslfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  177.                     LoadselectedDetails("xsl",includedxslfilenode,xslfileiterator);  
  178.                     RemoveUnwatedNodes(includedxslfilenode);  
  179.                     //TO Load all Class files of the selected Project...  
  180.                     XPathNodeIterator csfileiterator = nav.Select  
  181.                     (@"/VisualStudioProject/CSHARP/Files/Include/File");                        
  182.                     LoadselectedDetails("cs",includedcsfilenode,csfileiterator);  
  183.                     RemoveUnwatedNodes(includedcsfilenode);  
  184.                     //TO Load all User Controls files of the selected Project...  
  185.                     XPathNodeIterator usercntrlsfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  186.                     LoadselectedDetails("ascx.cs",includedusercntrlfilenode,usercntrlsfileiterator);  
  187.                     RemoveUnwatedNodes(includedusercntrlfilenode);  
  188.                     //TO Load all Unknown files of the selected Project...  
  189.                     XPathNodeIterator unknownsfileiterator = nav.Select(@"/VisualStudioProject/CSHARP/Files/Include/File");  
  190.                     while(unknownsfileiterator.MoveNext())  
  191.                     {  
  192.                         string filename = unknownsfileiterator.Current.GetAttribute("RelPath","").ToString().ToLower();  
  193.                         if(!filename.EndsWith(".aspx") && !filename.EndsWith(".aspx.cs") && !filename.EndsWith(".cs") && !  
  194.                             filename.EndsWith(".ascx.cs") && !filename.EndsWith(".js") &&  !filename.EndsWith(".xml") && !  
  195.                             filename.EndsWith(".xsl") && !filename.EndsWith(".css") &&  !filename.EndsWith(".gif") &&  !  
  196.                             filename.EndsWith (".config"))  
  197.                         {  
  198.                             includedunknownfilenode.Nodes.Add(filename.Substring(filename.LastIndexOf(@"\")+1));  
  199.                         }  
  200.                     }  
  201.                     RemoveUnwatedNodes(includedunknownfilenode);  
  202.                 }  
  203.                 else  
  204.                 {  
  205.                     TreeNode httpprjnode = new TreeNode(enprj.Value.ToString());  
  206.                     httpprjnode.ForeColor = Color.Red;  
  207.                     mainnode.Nodes.Add(httpprjnode);  
  208.                 }  
  209.             }  
  210.             if(enprj.Value.ToString().IndexOf("http://") != -1)  
  211.             {  
  212.                 httpprjs+=enprj.Value.ToString()+"#";  
  213.             }  
  214.         }  
  215.         foreach(string httpprj in httpprjs.Split(new char[]{'#'}))  
  216.         {  
  217.             if(httpprj.Trim() != "")  
  218.             {  
  219.                 TreeNode node = new TreeNode(httpprj.Trim());  
  220.                 mainnode.Nodes.Add(node);  
  221.             }  
  222.         }  
  223.     }  
  224. }  
  225. catch(Exception ex)  
  226. {  
  227.     MessageBox.Show(ex.Message);  
  228.     selfilecontents.Text = prevselfilecontents;  
  229. }  
  230. finally  
  231. {  
  232.     menuItem7.Enabled = true;  

In this click event, we are first determining whether the selected file is a project file or a solution file by using its extension. Since the Solution file will not be in XML format. So, I am using some text search patterns to get project details present in the solution file.
 
I am looping foreach project present in the solution, creating a temporary XML file to store project contents and then create a structure in a treeview with nodes named aspx, user controls like that we can add any number of nodes based on types of files present in your project. I am using XPath to navigate through project file present in XML format.
 
I am using a Xpath statement as @"/VisualStudioProject/CSHARP/Build/References/Reference" to get all project's references and @"/VisualStudioProject/CSHARP/Files/Include/File" to get all files included in project.
 
Based on their extension, I am adding each item to a specific node like adding .aspx files to ASPX Node. Then, I am removing nodes that don't have any items in them. Finally, I am formatting a treeview for a good look.
 
Then we are required to display the contents of selected files in the treeview. So, I am using the AfterSelect event of treeview, to display its contents.
 
Here, I am going to get the absolute physical path of the selected file using a HashTable which is created at that time of creating nodes foreach file type present in the project or solution. I am getting an absolute path of the selected file using the selected item's text and searching its path attribute in its corresponding project file. After getting its path, I am reading its entire contents and displaying it in a richtextbox.
 
Then add another form to get internal details of the selected reference. And create a UI as shown in the figure:
 
Img2.jpg
 
Here, I placed a menu, FileDialog, and tooltip followed by a treeview to display the selected reference details.
 
In Load Click, I am getting the selected reference path and using reflection, we are displaying its namespaces, classes, events, and methods in the treeview.
 
The final output will be like this:
 
Img3.jpg
 
Img4.jpg
 
We can still enhance this application with better UI and support of all file types and multiple language project file exploration.
 
I am attaching the code for further reference. I hope this code will be useful for all.