Click here to Skip to main content
15,894,955 members
Articles / Programming Languages / C#

A Small Tool to Remove SCC Information of a VS2003.NET Project File

Rate me:
Please Sign up or sign in to vote.
4.73/5 (25 votes)
15 Jul 20043 min read 190.7K   1.3K   60  
This tool removes the information saved in the project and solution files of VS2003.NET which are relative to source code control.
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
using System.DirectoryServices;

namespace RemoveSCCInfo
{
	/// <summary>
	/// Description r�sum�e de SccIntegrationRemovalLO.
	/// </summary>
	public sealed class SccIntegrationRemovalLO
	{
		private SccIntegrationRemovalLO()
		{
		}

		private static string ConvertWebProjectPathToLocalPath(string url)
		{
			// thanks goes to the author of VSPC for the 
			// idea of how to get the real path
			// http://jsp.mobikom.com/stoyan/vspc/vspc.html
			string fileName = null;
			try
			{
				Uri uri = new Uri(url);
				string adsiPath = String.Format("IIS://{0}/W3SVC/1/Root{1}", uri.Host,Path.GetDirectoryName(uri.AbsolutePath));
				DirectoryEntry dirEntry = new DirectoryEntry(adsiPath);
				string wwwPath = dirEntry.Properties["Path"].Value.ToString();
				fileName = Path.Combine(wwwPath,Path.GetFileName(uri.AbsolutePath));
			}
			catch
			{
			}
			return (fileName);
		}

		/// <summary>
		/// Parses the .sln file to get the projects
		/// </summary>
		/// <param name="solutionFilename"></param>
		/// <returns></returns>
		private static string [] GetProjectsFromSlnFile(string solutionFilename)
		{
			ArrayList arr = new ArrayList();
			string dir = Path.GetDirectoryName(solutionFilename)+@"\";
			Regex regex = new Regex(
				@"Project\(\""\{[A-Z|\d|\-|a-z]*\}\""\)\s*=\s*\""\S*\""\s*,\s*"
				+ @"\""(?<FileName>[^\""]*)\""\s*,\s*",
				RegexOptions.IgnoreCase
				| RegexOptions.Multiline
				| RegexOptions.IgnorePatternWhitespace
				| RegexOptions.Compiled
				);

			StreamReader rdr = null;

			try
			{
				rdr = new StreamReader(solutionFilename);
				string s = rdr.ReadLine();

				while(s!=null)
				{
					if(s=="Global") //Projects definition were before this point
						break; 

					Match mt = regex.Match(s);
					if(mt.Success)
					{
						string z = mt.Groups["FileName"].Value;
						if(z.StartsWith("http://"))
						{
							// if on localhost : use IIS to get the real path
							if(z.ToLower().StartsWith("http://localhost"))
							{
								arr.Add(ConvertWebProjectPathToLocalPath(z));
							}
							else
								throw new FileNotFoundException("IIS projects which are not on localhost are not yet supported",z);
						}
						else
						{
							z = Path.Combine(dir,z.Trim());
							arr.Add(z);
							if(!File.Exists(z))
								throw new FileNotFoundException("File not found",z);
						}
					}

					s = rdr.ReadLine();
				}
			}
			finally
			{
				if(rdr != null)
					rdr.Close();
			}

			return arr.ToArray(typeof(string)) as string[];

		}

		public static void CleanFiles(string rootFile, bool createBackup)
		{
			#region Predicates
			if(rootFile==null) throw new ArgumentNullException("rootFile");
			if(!File.Exists(rootFile)) throw new ArgumentException("The solution/project file must exist !");
			#endregion

			// checks if rootFile is a solution
			if(Path.GetExtension(rootFile.ToLower())==".sln")
				CleanSolution(rootFile,createBackup);
			else
				CleanProject(rootFile,createBackup);
		}

		/// <summary>
		/// Cleans a solution and its projects 
		/// </summary>
		/// <param name="solutionFile">path to the .sln file</param>
		/// <param name="createBackup">if true a backup of all modified files will be done</param>
		/// <exception cref="System.IO.FileNotFoundException">A project can not be found (not here or a web project)</exception>
		/// <exception cref="System.ApplicationException">A project of an unsupported type has been found</exception>
		private static void CleanSolution(string solutionFile, bool createBackup)
		{
			// THIS may raise FileNotFoundException if a web-project is in the solution
			// or an ApplicationException if there are unsupported project types
			string[] prjs = GetProjectsFromSlnFile(solutionFile);
			foreach(string s in prjs)
				CheckValidProjectFile(s);

			// alter the sln file and deletes suo and vssscc
			RemoveSccFrom2003SolutionCommand cmd = new RemoveSccFrom2003SolutionCommand(solutionFile,createBackup);
			cmd.Do();

			// clean the projects that are part of the solution
			foreach(string s in prjs)
			{
				RemoveSccFrom2003ProjectCommand cmdPrj = new RemoveSccFrom2003ProjectCommand(s,createBackup);
				cmdPrj.Do();
			}
		}

		/// <summary>
		/// Cleans a project file
		/// </summary>
		/// <param name="solutionFile">path to the .csproj or .vbproj file</param>
		/// <param name="createBackup">if true a backup of all modified files will be done</param>
		private static void CleanProject(string projectFile, bool createBackup)
		{
			CheckValidProjectFile(projectFile);

			RemoveSccFrom2003ProjectCommand cmd = new RemoveSccFrom2003ProjectCommand(projectFile,createBackup);
			cmd.Do();
		}

		/// <summary>
		/// Validates a project file
		/// </summary>
		/// <param name="projectFile">File to check</param>
		/// <exception cref="System.ApplicationException">Unsupported project-type has been found</exception>
		private static void CheckValidProjectFile(string projectFile)
		{
			switch(Path.GetExtension(projectFile.ToLower()))
			{
				case ".csproj":
					break;
				case ".vbproj":
					break;
				case ".vdproj":
					break;
				case ".vcproj":
					break;
				default:
					throw new ApplicationException("Only .csproj and .vbproj files are supported, the scc-cleaning of other types of project is not yet supported !");
			}
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
France France
I'm french, do I need to say more ?

Comments and Discussions