Imagine that you have a couple of project files that reference framework libraries that are on a buildserver. Upgrading to a newer version requires that you update all the references… I wrote some wrapper classes (ProjectFile, AssemblyReference) that make this tedious task a breeze. Here is an example of their usage:
string path = "D:\\Projects\\MyProject";
string[] projectFileNames = ProjectFile.Find(path);
foreach (string projectFileName in projectFileNames)
{
ProjectFile projectFile = new ProjectFile(projectFileName);
bool updated = false;
foreach (AssemblyReference assemblyReference in projectFile.AssemblyReferences)
{
if (assemblyReference.HintPath.ToLower().StartsWith("\\\\buildserver\\framework\\2.0"))
{
string newHintPath = assemblyReference.HintPath.Replace("\\2.0\\", "\\2.1\\");
assemblyReference.HintPath = newHintPath;
AssemblyName assemblyName = AssemblyName.GetAssemblyName(assemblyReference.HintPath);
assemblyReference.AssemblyName = assemblyName.FullName + ", processorArchitecture=" + assemblyName.ProcessorArchitecture;
updated = true;
}
}
if (updated)
{
projectFile.Save();
}
}
Edit: Be careful because it might be possible that the code changes the encoding of your csproj file (and it seems that the TFS 2005 merge tool doesn’t like that). Currently files are written as UTF-8, which is the default for VS2005 csproj files.
Hi Tim,
The article is very nice and the code was very nicely structured.
I have one question related to the CSProj file.
How can i get the output path from the CSProj file?
By default it will be bin\debug, but if change the build configuration it will different like bin\release or bin\x64\debug etc.
Thank You
-Giri