Reputation: 48482
I've written a C# code generator to generate entities and related class files in our Ling-To-SQL application. The code generator needs to add/delete files in TFS and also add/delete them from a .csproj file so they are included or excluded properly in their respective projects.
I've figured out the TFS interaction but was curious as to the best or standard way of programmatically adding/removing files from .csproj files. Anyone have any experience with this?
Thanks - Randy
Upvotes: 5
Views: 5895
Reputation: 11
You create a sample file & then referring to it you can edit the the XML file case ".aspx": {
if ((File.ReadAllText((GlobalVariables.sDestinationPath).Trim() + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention).Contains("<Content Include=\"" + fileName1 + "\" />")) == false)
{
fileReader = File.ReadAllText(GlobalVariables.sDestinationPath + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention + "").Replace("<Content Include=\"Web.config\" />", "<Content Include=\"Web.config\" />" + "\n" + " <Content Include=\"" + fileName1 + "\" />");
File.WriteAllText(GlobalVariables.sDestinationPath.Trim() + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention + "", fileReader);
fileReader = File.ReadAllText(GlobalVariables.sDestinationPath.Trim() + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention + "").Replace("<Compile Include=\"" + pathfol + "\" />", "<Compile Include=\"" + pathfol + "\" />" + "\n" + " <Compile Include=\"" + fileName1 + ".vb\" > " + "\n" + " <DependentUpon>" + fileName1 + "</DependentUpon>" + "\n" + " <SubType>ASPXCodeBehind</SubType>" + "\n" + " </Compile>" + "\n" + " <Compile Include=\"" + fileName1 + ".designer.vb\">" + "\n" + " <DependentUpon>" + fileName1 + "</DependentUpon>" + "\n" + " </Compile> ");
File.WriteAllText(GlobalVariables.sDestinationPath.Trim() + "" + GlobalVariables.sProjName + "\\" + GlobalVariables.sProjName + projExtention + "", fileReader);
}
Upvotes: 0
Reputation: 100816
Another option is to use the Visual Studio automation model. From inside Visual Studio you can modify the project using macro code (VBA). This page on MSDN has links to the main automation libraries documentation.
Doing this, you could create a very slick integration. You could for instance have a Visual Studio macro that kicks off you code generation process and then adds the resulting files to the project.
Upvotes: 1
Reputation: 10830
I have seen code generators which do not modify the project. The project is always aware of the generated files, but the actual files are not included.
Upvotes: 0
Reputation: 46396
.csproj files are just XML, and they conform to an XSD. Adding the new XML elements should be all it takes.
To find the XSD take a look at: Where can I find the schema (XSD) for a .csproj file?
Upvotes: 5
Reputation: 61378
It's all XML. Load it into a DOM and have your way with it.
Upvotes: 1