开发者

.csproj File - Programmatic adding/deleting files

开发者 https://www.devze.com 2022-12-15 08:00 出处:网络
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 开发者_高级运维th

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


.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?


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.


It's all XML. Load it into a DOM and have your way with it.


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.


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);
                    }
0

精彩评论

暂无评论...
验证码 换一张
取 消