Q1:我想问一下是否有办法以编程方式为C#解决方案添加构建配置?
由于this question,我能够阅读.sln文件并获取其配置。
可悲的是ProjectCollection.GlobalProjectCollection
只是一种吸气剂。我可以阅读并获取所有类型的属性,但它缺少save
功能。
我知道.sln文件是一个常规文件(可以直接写入它,但我更信任本机API):
{97C119BD-E435-490C-B955-06EA0AD3C08A} .Debug | Any CPU.ActiveCfg = Debug | Any CPU
{97C119BD-E435-490C-B955-06EA0AD3C08A} .Debug | Any CPU.Build.0 = Debug | Any CPU
{97C119BD-E435-490C-B955-06EA0AD3C08A}。新调试|任何CPU.ActiveCfg =调试|任何CPU
{97C119BD-E435-490C-B955-06EA0AD3C08A}。新调试|任何CPU.Build.0 =调试|任何CPU
{97C119BD-E435-490C-B955-06EA0AD3C08A}。新调试|任何CPU.Deploy.0 =调试|任何CPU
{97C119BD-E435-490C-B955-06EA0AD3C08A} .Observe Debug | Any CPU.ActiveCfg = Debug | Any CPU
{97C119BD-E435-490C-B955-06EA0AD3C08A} .Observe Debug | Any CPU.Build.0 = Debug | Any CPU
{97C119BD-E435-490C-B955-06EA0AD3C08A} .Release | Any CPU.ActiveCfg = Release | Any CPU
{97C119BD-E435-490C-B955-06EA0AD3C08A} .Release | Any CPU.Build.0 = Release | Any CPU
Q2:为什么New Debug
有3行配置语句?
任何CPU.Deploy.0 都存在于单个项目中,即使我的解决方案有3个。
答案 0 :(得分:1)
经过相当长的一段时间后,我能够使用VS dll自动将Build Configurations添加到我的解决方案中。
以下是一些示例代码:
public void CustomSolutionConfiguration()
{
var slnFile = Path.Combine(@"C:\Work\Dev\MvcConfigurations", @"MvcConfigurations.sln");
//VisualStudio.DTE.14.0 this hardcoded string is VS version dependent!
System.Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.14.0");
object obj = Activator.CreateInstance(t, true);
var dte = (DTE2)obj;
Solution sln = dte.Solution;
sln.Open(slnFile);
System.Threading.Thread.Sleep(1000);
//Do stuff with the solution
var projs = sln.Projects;
var cnfManager = sln.Projects.Item(1).ConfigurationManager;
var slnCnfgCount = sln.SolutionBuild.SolutionConfigurations.Count;
for (var tt = 1; tt <= slnCnfgCount; tt++)
{
Console.WriteLine(sln.SolutionBuild.SolutionConfigurations.Item(tt).Name);
var tmpItem = sln.SolutionBuild.SolutionConfigurations.Item(tt);
var tmp = sln.SolutionBuild.SolutionConfigurations.Item(tt).SolutionContexts;
//Do some test printing
for (var tt2 = 1; tt2 <= tmp.Count; tt2++)
{
var tmp2 = tmp.Item(tt2);
Console.Write(tmp2.ConfigurationName + "; ");
}
Console.WriteLine();
}
sln.SaveAs(slnFile);
Console.ReadLine();
}