问题
The design based approach is: New Project -> Other Project Type -> Visual Studio Solution -> Blank Solution
I have to create a blank solution programmatically in C# and in this solution add new empty project and files. i found a lot of code on the web using DTE but they are adding my empty project in existing solution explorer so please give me some reference code.
回答1:
You can create a new blank solution using DTE like this:
string visualStudioProgID = "VisualStudio.Solution.9.0";
Type solutionObjectType = System.Type.GetTypeFromProgID(visualStudioProgID, true);
object obj = System.Activator.CreateInstance(solutionObjectType, true);
Solution3 solutionObject = (Solution3)obj;
solutionObject.Create(".", "MySolution");
solutionObject.SaveAs(@"C:\Temp\MySolution.sln"); //or wherever you prefer
You have to add references to EnvDTE.dll, EnvDTE80.dll, and EnvDTE90.dll. The resulting file you will get is very simple and could be created in other ways (as a plain text file).
回答2:
If you truly just need to create a solution with an empty project file it's probably much easier to just write the .sln file (it's a plain text file) and the .csproj file (it's an XML MSBuild file) by hand than it is to mess with Visual Studio automation.
回答3:
Create a solution with empty project manually. Replace project/solution name/other specific data with {n} so you can fill them using string.Format later. Then embed those files (sln and csproj) to your assembly and use them as resource.
来源:https://stackoverflow.com/questions/5037995/how-can-i-create-new-blank-solution-in-vs-2008-programmatically