1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
4 ' Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright 2000-2002 Philip A. Craig
7 ' This software is provided 'as-is', without any express or implied warranty. In no
8 ' event will the authors be held liable for any damages arising from the use of this
11 ' Permission is granted to anyone to use this software for any purpose, including
12 ' commercial applications, and to alter it and redistribute it freely, subject to the
13 ' following restrictions:
15 ' 1. The origin of this software must not be misrepresented; you must not claim that
16 ' you wrote the original software. If you use this software in a product, an
17 ' acknowledgment (see the following) in the product documentation is required.
19 ' Portions Copyright 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright 2000-2002 Philip A. Craig
22 ' 2. Altered source versions must be plainly marked as such, and must not be
23 ' misrepresented as being the original software.
25 ' 3. This notice may not be removed or altered from any source distribution.
27 '***********************************************************************************/
37 /// This class allows loading information about
38 /// configurations and assemblies in a Visual
39 /// Studio project file and inspecting them.
40 /// Only the most common project types are
41 /// supported and an exception is thrown if
42 /// an attempt is made to load an invalid
43 /// file or one of an unknown type.
45 public class VSProject
47 #region Static and Instance Variables
50 /// VS Project extentions
52 private static readonly string[] validExtensions = { ".csproj", ".vbproj", ".vjsproj", ".vcproj" };
55 /// VS Solution extension
57 private static readonly string solutionExtension = ".sln";
60 /// Path to the file storing this project
62 private string projectPath;
65 /// Collection of configs for the project
67 private VSProjectConfigCollection configs;
73 public VSProject( string projectPath )
75 this.projectPath = Path.GetFullPath( projectPath );
76 configs = new VSProjectConfigCollection();
86 /// The name of the project.
90 get { return Path.GetFileNameWithoutExtension( projectPath ); }
94 /// The path to the project
96 public string ProjectPath
98 get { return projectPath; }
102 /// Our collection of configurations
104 public VSProjectConfigCollection Configs
106 get { return configs; }
111 #region Static Methods
113 public static bool IsProjectFile( string path )
115 string extension = Path.GetExtension( path );
117 foreach( string validExtension in validExtensions )
118 if ( extension == validExtension )
124 public static bool IsSolutionFile( string path )
126 return Path.GetExtension( path ) == solutionExtension;
131 #region Instance Methods
135 if ( !IsProjectFile( projectPath ) )
136 ThrowInvalidFileType( projectPath );
138 string projectDirectory = Path.GetFullPath( Path.GetDirectoryName( projectPath ) );
142 XmlDocument doc = new XmlDocument();
143 doc.Load( projectPath );
145 string extension = Path.GetExtension( projectPath );
151 foreach ( XmlNode configNode in doc.SelectNodes( "/VisualStudioProject/Configurations/Configuration" ) )
153 string name = configNode.Attributes["Name"].Value;
154 string outputPath = configNode.Attributes["OutputDirectory"].Value;
155 string outputDirectory = Path.Combine( projectDirectory, outputPath );
156 XmlNode toolNode = configNode.SelectSingleNode( "Tool[@Name='VCLinkerTool']" );
157 assemblyName = Path.GetFileName( toolNode.Attributes["OutputFile"].Value );
158 string assemblyPath = Path.Combine( outputDirectory, assemblyName );
160 VSProjectConfig config = new VSProjectConfig ( name );
161 config.Assemblies.Add( assemblyPath );
163 this.configs.Add( config );
171 XmlNode settingsNode = doc.SelectSingleNode( "/VisualStudioProject/*/Build/Settings" );
173 assemblyName = settingsNode.Attributes["AssemblyName"].Value;
174 string outputType = settingsNode.Attributes["OutputType"].Value;
176 if ( outputType == "Exe" || outputType == "WinExe" )
177 assemblyName = assemblyName + ".exe";
179 assemblyName = assemblyName + ".dll";
181 XmlNodeList nodes = settingsNode.SelectNodes("Config");
183 foreach ( XmlNode configNode in nodes )
185 string name = configNode.Attributes["Name"].Value;
186 string outputPath = configNode.Attributes["OutputPath"].Value;
187 string outputDirectory = Path.Combine( projectDirectory, outputPath );
188 string assemblyPath = Path.Combine( outputDirectory, assemblyName );
190 VSProjectConfig config = new VSProjectConfig ( name );
191 config.Assemblies.Add( assemblyPath );
193 configs.Add( config );
202 catch( FileNotFoundException )
208 ThrowInvalidFormat( projectPath, e );
212 private void ThrowInvalidFileType( string projectPath )
214 throw new ArgumentException(
215 string.Format( "Invalid project file type: {0}",
216 Path.GetFileName( projectPath ) ) );
219 private void ThrowInvalidFormat( string projectPath, Exception e )
221 throw new ArgumentException(
222 string.Format( "Invalid project file format: {0}",
223 Path.GetFileName( projectPath ) ), e );