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 ) );
139 string currentDirectory = Environment.CurrentDirectory;
143 XmlDocument doc = new XmlDocument();
144 doc.Load( projectPath );
146 string extension = Path.GetExtension( projectPath );
152 foreach ( XmlNode configNode in doc.SelectNodes( "/VisualStudioProject/Configurations/Configuration" ) )
154 string name = configNode.Attributes["Name"].Value;
155 string outputPath = configNode.Attributes["OutputDirectory"].Value;
156 string outputDirectory = Path.Combine( projectDirectory, outputPath );
157 XmlNode toolNode = configNode.SelectSingleNode( "Tool[@Name='VCLinkerTool']" );
158 assemblyName = Path.GetFileName( toolNode.Attributes["OutputFile"].Value );
159 string assemblyPath = Path.Combine( outputDirectory, assemblyName );
161 VSProjectConfig config = new VSProjectConfig ( name );
162 config.Assemblies.Add( assemblyPath );
164 this.configs.Add( config );
172 XmlNode settingsNode = doc.SelectSingleNode( "/VisualStudioProject/*/Build/Settings" );
174 assemblyName = settingsNode.Attributes["AssemblyName"].Value;
175 string outputType = settingsNode.Attributes["OutputType"].Value;
177 if ( outputType == "Exe" || outputType == "WinExe" )
178 assemblyName = assemblyName + ".exe";
180 assemblyName = assemblyName + ".dll";
182 XmlNodeList nodes = settingsNode.SelectNodes("Config");
184 foreach ( XmlNode configNode in nodes )
186 string name = configNode.Attributes["Name"].Value;
187 string outputPath = configNode.Attributes["OutputPath"].Value;
188 string outputDirectory = Path.Combine( projectDirectory, outputPath );
189 string assemblyPath = Path.Combine( outputDirectory, assemblyName );
191 VSProjectConfig config = new VSProjectConfig ( name );
192 config.Assemblies.Add( assemblyPath );
194 configs.Add( config );
203 catch( FileNotFoundException )
209 ThrowInvalidFormat( projectPath, e );
213 private void ThrowInvalidFileType( string projectPath )
215 throw new ArgumentException(
216 string.Format( "Invalid project file type: {0}",
217 Path.GetFileName( projectPath ) ) );
220 private void ThrowInvalidFormat( string projectPath, Exception e )
222 throw new ArgumentException(
223 string.Format( "Invalid project file format: {0}",
224 Path.GetFileName( projectPath ) ), e );