如何从 Visual Studio 扩展和新的 csproj 格式中获取 IntermediateOutputPath
How to get IntermediateOutputPath from Visual Studio extension and new csproj format
我有一个覆盖 IntermediateOutputPath
的新样式 csproj
项目文件。它看起来像这样:
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<IntermediateOutputPath>new\path\to\obj</IntermediateOutputPath>
</PropertyGroup>
问题是,我的 Visual Studio 分机无法访问 IntermediateOutputPath
属性。 Project.Properties
与旧项目格式相比,内容似乎少了很多。
我也试过 project.ConfigurationManager.ActiveConfiguration.Properties
也取得了类似的成功。
有什么方法可以从 Visual Studio 扩展程序中获取此信息?
所以我创建了一个简单的扩展来打印所有属性
private string GetPropertiesString(Properties properties)
{
StringBuilder test = new StringBuilder();
foreach (Property property in properties)
{
try
{
test.AppendLine(property.Name + ":=" + property.Value.ToString());
Console.WriteLine(property.Name + ":=" + property.Value.ToString());
}
catch (Exception ex)
{
var x = ex.Message;
}
}
return test.ToString();
}
private void MenuItemCallback(object sender, EventArgs e)
{
DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;
var sol = dte2.Solution;
var projs = sol.Projects;
foreach (var proj in sol)
{
var project = proj as Project;
var rows = project.ConfigurationManager.ConfigurationRowNames as IEnumerable<object>;
foreach (var row in rows)
{
var config = project.ConfigurationManager.ConfigurationRow(row.ToString()).Item(1) as Configuration;
string configs = GetPropertiesString(config.Properties);
}
}
}
这给出了以下输出
LanguageVersion:=
RunCodeAnalysis:=False
NoStdLib:=False
ErrorReport:=prompt
CodeAnalysisUseTypeNameInSuppression:=True
CodeAnalysisInputAssembly:=bin\Debug\WindowsFormsApp1.exe
CodeAnalysisDictionaries:=
GenerateSerializationAssemblies:=2
CodeAnalysisModuleSuppressionsFile:=GlobalSuppressions.cs
StartWorkingDirectory:=
Optimize:=False
DocumentationFile:=
StartPage:=
OutputPath:=bin\Debug\
TreatWarningsAsErrors:=False
EnableASPDebugging:=False
IncrementalBuild:=True
CodeAnalysisFailOnMissingRules:=False
CodeAnalysisLogFile:=bin\Debug\WindowsFormsApp1.exe.CodeAnalysisLog.xml
DefineConstants:=DEBUG;TRACE
UseVSHostingProcess:=True
StartProgram:=
DefineDebug:=False
CodeAnalysisIgnoreBuiltInRules:=True
CodeAnalysisRuleSetDirectories:=;F:\VS2017\Team Tools\Static Analysis Tools\Rule Sets
CodeAnalysisCulture:=
CodeAnalysisOverrideRuleVisibilities:=False
CodeAnalysisRuleAssemblies:=
DefineTrace:=False
DebugSymbols:=True
CodeAnalysisIgnoreBuiltInRuleSets:=True
CodeAnalysisRuleSet:=MinimumRecommendedRules.ruleset
NoWarn:=
CodeAnalysisIgnoreGeneratedCode:=True
EnableSQLServerDebugging:=False
BaseAddress:=4194304
RemoteDebugEnabled:=False
StartURL:=
AllowUnsafeBlocks:=False
TreatSpecificWarningsAsErrors:=
PlatformTarget:=AnyCPU
EnableUnmanagedDebugging:=False
StartWithIE:=False
StartArguments:=
IntermediatePath:=new\path\to\obj2\
CodeAnalysisRuleDirectories:=;F:\VS2017\Team Tools\Static Analysis Tools\FxCop\Rules
DebugInfo:=full
CheckForOverflowUnderflow:=False
RemoteDebugMachine:=
Prefer32Bit:=True
CodeAnalysisSpellCheckLanguages:=
CodeAnalysisRules:=
RegisterForComInterop:=False
FileAlignment:=512
StartAction:=0
EnableASPXDebugging:=False
ConfigurationOverrideFile:=
WarningLevel:=4
RemoveIntegerChecks:=False
在我添加的CS项目中
<IntermediateOutputPath>new\path\to\obj2</IntermediateOutputPath>
如您所见,IntermediateOutputPath
即将成为 IntermediatePath
。所以你可以使用
var config = project.ConfigurationManager.ConfigurationRow("Debug").Item(1) as Configuration;
config.Properties.Item("IntermediatePath").Value
Edit-1 - .NET 标准项目
Edit-2 - 2017 年 8 月 12 日
因此,在深入研究该问题后,我发现 属性 本身就是一个 MSBuild 属性,而不是与 CSProject 相关的 属性。这就是您在 Properties 属性中看不到它的原因。这需要稍微不同的方向才能使用 IVsBuildPropertyStorage.GetPropertyValue
获取值
private IVsBuildPropertyStorage GetBuildPropertyStorage(EnvDTE.Project project)
{
IVsSolution solution = (IVsSolution)ServiceProvider.GetService(typeof(SVsSolution));
IVsHierarchy hierarchy;
int hr = solution.GetProjectOfUniqueName(project.FullName, out hierarchy);
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);
return hierarchy as IVsBuildPropertyStorage;
}
private string GetBuildProperty(string key, IVsBuildPropertyStorage Storage)
{
string value;
int hr = Storage.GetPropertyValue(key, null, (uint)_PersistStorageType.PST_USER_FILE, out value);
int E_XML_ATTRIBUTE_NOT_FOUND = unchecked((int)0x8004C738);
// ignore this HR, it means that there's no value for this key
if (hr != E_XML_ATTRIBUTE_NOT_FOUND)
{
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);
}
return value;
}
然后再使用这些方法获取值
var project = proj as EnvDTE.Project;
IVsBuildPropertyStorage storage = GetBuildPropertyStorage(project);
string outputPath = GetBuildProperty("IntermediateOutputPath", storage);
这给了我 属性
的正确值
我有一个覆盖 IntermediateOutputPath
的新样式 csproj
项目文件。它看起来像这样:
<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<IntermediateOutputPath>new\path\to\obj</IntermediateOutputPath>
</PropertyGroup>
问题是,我的 Visual Studio 分机无法访问 IntermediateOutputPath
属性。 Project.Properties
与旧项目格式相比,内容似乎少了很多。
我也试过 project.ConfigurationManager.ActiveConfiguration.Properties
也取得了类似的成功。
有什么方法可以从 Visual Studio 扩展程序中获取此信息?
所以我创建了一个简单的扩展来打印所有属性
private string GetPropertiesString(Properties properties)
{
StringBuilder test = new StringBuilder();
foreach (Property property in properties)
{
try
{
test.AppendLine(property.Name + ":=" + property.Value.ToString());
Console.WriteLine(property.Name + ":=" + property.Value.ToString());
}
catch (Exception ex)
{
var x = ex.Message;
}
}
return test.ToString();
}
private void MenuItemCallback(object sender, EventArgs e)
{
DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2;
var sol = dte2.Solution;
var projs = sol.Projects;
foreach (var proj in sol)
{
var project = proj as Project;
var rows = project.ConfigurationManager.ConfigurationRowNames as IEnumerable<object>;
foreach (var row in rows)
{
var config = project.ConfigurationManager.ConfigurationRow(row.ToString()).Item(1) as Configuration;
string configs = GetPropertiesString(config.Properties);
}
}
}
这给出了以下输出
LanguageVersion:=
RunCodeAnalysis:=False
NoStdLib:=False
ErrorReport:=prompt
CodeAnalysisUseTypeNameInSuppression:=True
CodeAnalysisInputAssembly:=bin\Debug\WindowsFormsApp1.exe
CodeAnalysisDictionaries:=
GenerateSerializationAssemblies:=2
CodeAnalysisModuleSuppressionsFile:=GlobalSuppressions.cs
StartWorkingDirectory:=
Optimize:=False
DocumentationFile:=
StartPage:=
OutputPath:=bin\Debug\
TreatWarningsAsErrors:=False
EnableASPDebugging:=False
IncrementalBuild:=True
CodeAnalysisFailOnMissingRules:=False
CodeAnalysisLogFile:=bin\Debug\WindowsFormsApp1.exe.CodeAnalysisLog.xml
DefineConstants:=DEBUG;TRACE
UseVSHostingProcess:=True
StartProgram:=
DefineDebug:=False
CodeAnalysisIgnoreBuiltInRules:=True
CodeAnalysisRuleSetDirectories:=;F:\VS2017\Team Tools\Static Analysis Tools\Rule Sets
CodeAnalysisCulture:=
CodeAnalysisOverrideRuleVisibilities:=False
CodeAnalysisRuleAssemblies:=
DefineTrace:=False
DebugSymbols:=True
CodeAnalysisIgnoreBuiltInRuleSets:=True
CodeAnalysisRuleSet:=MinimumRecommendedRules.ruleset
NoWarn:=
CodeAnalysisIgnoreGeneratedCode:=True
EnableSQLServerDebugging:=False
BaseAddress:=4194304
RemoteDebugEnabled:=False
StartURL:=
AllowUnsafeBlocks:=False
TreatSpecificWarningsAsErrors:=
PlatformTarget:=AnyCPU
EnableUnmanagedDebugging:=False
StartWithIE:=False
StartArguments:=
IntermediatePath:=new\path\to\obj2\
CodeAnalysisRuleDirectories:=;F:\VS2017\Team Tools\Static Analysis Tools\FxCop\Rules
DebugInfo:=full
CheckForOverflowUnderflow:=False
RemoteDebugMachine:=
Prefer32Bit:=True
CodeAnalysisSpellCheckLanguages:=
CodeAnalysisRules:=
RegisterForComInterop:=False
FileAlignment:=512
StartAction:=0
EnableASPXDebugging:=False
ConfigurationOverrideFile:=
WarningLevel:=4
RemoveIntegerChecks:=False
在我添加的CS项目中
<IntermediateOutputPath>new\path\to\obj2</IntermediateOutputPath>
如您所见,IntermediateOutputPath
即将成为 IntermediatePath
。所以你可以使用
var config = project.ConfigurationManager.ConfigurationRow("Debug").Item(1) as Configuration;
config.Properties.Item("IntermediatePath").Value
Edit-1 - .NET 标准项目
Edit-2 - 2017 年 8 月 12 日
因此,在深入研究该问题后,我发现 属性 本身就是一个 MSBuild 属性,而不是与 CSProject 相关的 属性。这就是您在 Properties 属性中看不到它的原因。这需要稍微不同的方向才能使用 IVsBuildPropertyStorage.GetPropertyValue
private IVsBuildPropertyStorage GetBuildPropertyStorage(EnvDTE.Project project)
{
IVsSolution solution = (IVsSolution)ServiceProvider.GetService(typeof(SVsSolution));
IVsHierarchy hierarchy;
int hr = solution.GetProjectOfUniqueName(project.FullName, out hierarchy);
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);
return hierarchy as IVsBuildPropertyStorage;
}
private string GetBuildProperty(string key, IVsBuildPropertyStorage Storage)
{
string value;
int hr = Storage.GetPropertyValue(key, null, (uint)_PersistStorageType.PST_USER_FILE, out value);
int E_XML_ATTRIBUTE_NOT_FOUND = unchecked((int)0x8004C738);
// ignore this HR, it means that there's no value for this key
if (hr != E_XML_ATTRIBUTE_NOT_FOUND)
{
System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);
}
return value;
}
然后再使用这些方法获取值
var project = proj as EnvDTE.Project;
IVsBuildPropertyStorage storage = GetBuildPropertyStorage(project);
string outputPath = GetBuildProperty("IntermediateOutputPath", storage);
这给了我 属性
的正确值