Visual Studio Addin "Exclude From Build" Property - c#

I am currently trying to create an addin for Visual Studio 2008 that will list all files which are not excluded from the current build configuration.
I currently have test C++ console application that has 10 files, 2 of which are "Excluded From Build". This is a property that will allow a specific file to be excluded from a specific configuration (i.e. Debug or Release). This property is located when you right click on a file in the solution explorer and select Properties->Configuration Properties->General->Excluded From Build
At the moment I have the following code that will loop though all project files and get the properties for each file.
foreach (Project theProject in _applicationObject.Solution.Projects)
{
getFiles(theProject.ProjectItems);
}
private void getFiles(ProjectItems theItems)
{
foreach (ProjectItem theItem in theItems)
{
string theItemName = theItem.Name;
foreach (Property theProp in theItem.Properties)
{
string thePropName = theProp.Name;
}
getFiles(theItem.ProjectItems);
}
}
The issue I am having is that I cant seem to find the "Excluded From Build" property. I cannot find very good documentation on what properties are listed where. Where is this Excluded From Build property located within the _applicationObject object?

I'm not familiar with the Visual Studio object model, but in the documentation for VS2005 the following objects have an ExcludedFromBuild property:
VCFileConfiguration
VCFileConfigurationProperties
VCPreBuildEventTool
VCPreLinkEventTool
VCPostBuildEventTool
VCWebDeploymentTool
Hopefully this will lead you down the right path.

Related

How to get "Include Paths" property of Microsoft Macro Assembler in Visual Studio by a plugin?

I'm workng to get include path resolved by some VS plugin (asm-dude in fact). Include path in microsoft macro assembler looks like this:
includepath
Include file resolve part in asm-dude lies in: https://github.com/HJLebbink/asm-dude/blob/vxix2022-B/VS/CSHARP/asm-dude-vsix/Tools/LabelGraph.cs#L602
Anyway, at the beginning I think I just need to get the value of IncludePath property, and then other things can be done in a minute. But after reading some docs I realized I'm in a mess. It seems that VS prevents me to get names of all properties, but I can only get the value by the name.
Codes I write are like:
DTE dte = Package.GetGlobalService(typeof(SDTE)) as DTE;
Projects projects = dte.Solution.Projects;
if (projects.Count != 0)
{
VCProject project = (VCProject)projects.Item(1).Object;
VCConfiguration cfg = project.ActiveConfiguration;
if (cfg != null)
{
string includePathStr = cfg.GetEvaluatedPropertyValue("IncludePaths");
}
}
but in vein, it gets include path of msvc, not MASM
I cast Project to VCProject because it's a VC project. Although I can iterate properties of a non-VCProject's configuration, but it doesn't seem to work on VCProject, because it doesn't have a (at least not public) member named properties. All these docs tell me that I can only get its value by name, but the problem is I don't know its name. Or I'm completely wrong? I must admit that I'm new to VS plugins.
refs I used so far:
https://learn.microsoft.com/en-us/previous-versions/dn655034(v=vs.140)?redirectedfrom=MSDN
https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.vcprojectengine.vcconfiguration?view=visualstudiosdk-2022

Add ClassLibrary Project programmatically

I have a VS 2017 extension and have tried using my own custom Project Template, by adding it programmatically, but things aren't going so well.
In my endeavour to find the mistake, I would like to see whether it is my custom Project Template causing the problem or not. Therefore I want to programmatically add any other existing built-in VS project such as a ClassLibrary type project template.
It seems to be located here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ProjectTemplates\CSharp\Windows\1033\
But there isn't a zip folder and I can't create one in that directory according to Windows.
I will be using something similar to the following code:
Solution2 soln = (Solution2)visualStudioInstance.Solution;
string templatePath = soln.GetProjectTemplate("ClassLibrary.zip", "CSharp");
soln.AddFromTemplate(templatePath, projPath, "MyProjectName", false);
Am I on the right track?
I've tried it and I got an exception, but perhaps it was just because the zip folder doesn't exist.
UPDATE
The exception I get is:
"Object reference not set to an instance of an object."
I get it in the last line of the following code, when I try to add a reference to calcEngineProject. calcEngineProject is null even though it enters the if-statement and should be assigned the value of projCS.Object as VSProject2.
The code is as follows:
templatePath = soln.GetProjectTemplate("ClassLibrary.zip", "CSharp");
soln.AddFromTemplate(templatePath, prjPath, "ClassLibrary1", false);
foreach (Project p in soln.Projects)
{
if (String.Compare(p.Name, "ClassLibrary1") == 0)
{
projCS = p;
break;
}
}
if (projCS != null)
{
calcEngineProject = projCS.Object as VSProject2;
}
calcEngineProject.References.Add(Path.Combine(Config.Engine.EngineBinPath, "Engines.Calculation.dll"));
Also, I saw that templatePath is this:
"C:\PROGRAM FILES (X86)\MICROSOFT VISUAL
STUDIO\2017\PROFESSIONAL\COMMON7\IDE\EXTENSIONS\EYXTAMKA.FB4\ProjectTemplates\CSharp\.NET
Standard\1033\ClassLibrary\ClassLibrary.vstemplate"
and not
\%USERPROFILE%\Documents\My Exported Templates\
as mentioned in Upgrading custom project template

Change VS option by using the Settings Store: finding the right CollectionPath?

I need to set the value of a Visual Studio option found in Visual Studio -> Tools -> Options -> Text Editor -> JavaScript/TypeScript -> EsLint but I can't seem to find the CollectionPath for this option.
GetSubCollectionNames("Text Editor"); yield a number of results, while GetSubCollectionNames("Text Editor\\JavaScript"); yield 0 results.
TL;DR
How would one go about finding the right CollectionPath for the option pictured in the image below?
This is what I'm using currently.
[ImportingConstructor]
internal VSOptions([Import] SVsServiceProvider serviceProvider)
{
var settingsManager = new ShellSettingsManager(serviceProvider);
_writableSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings)
?? throw new Exception(nameof(settingsManager));
var textEditorSubCollections = _writableSettingsStore.GetSubCollectionNames("Text Editor");
var javaScriptSubCollections = _writableSettingsStore.GetSubCollectionNames("Text Editor\\JavaScript");
// TODO: set option value when we have the right CollectionPath
}
The WritabelSettingsStore class used to extend Visual Studio common settings in Visual Studio. You could use GetPropertyNames("Text Editor\JavaScript") to list all writabel settings for JavaScript, where you will find not all Properties under JavaScript sub collections are listed.
The EsLint is not common Visual Studio Settings. It is third part tool for identifying and reporting on patterns found in ECMAScript/JavaScript code, with the goal of making code more consistent and avoiding bugs.
So we could not change it directly with WritableSettingsStore class. You need to know how the EsLint added in Visual Studio and then modify its configuration file for Visual Studio.

Visual Studio Wizard - Won't add project items to the correct folder

I'm trying to create a custom Visual Studio wizard to create generated code.
Everything works well but the only problem is that the project file won't be saved in right folder. I'm right clicking on a project folder and selecting 'New Item...' but the wizard saves the file in project root.
The code:
public class ProcedureClass : IDTWizard
{
public void Execute(object Application, int hwndOwner, ref object[] ContextParams, ref object[] CustomParams, ref EnvDTE.wizardResult retval)
{
// Showing wizard dialog and generating code here...
var projectItems = ContextParams[2] as ProjectItems;
var project = projectItems.ContainingProject;
retval = wizardResult.wizardResultSuccess;
project.ProjectItems.AddFromTemplate(tempFile, ContextParams[4]);
}
}
Here is an image showing where I clicked and where the file has been created:
This is expected given that your code uses projectItems.ContainingProject, which always returns the root project.
See my article
HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in
http://www.mztools.com/articles/2006/MZ2006004.aspx
The correct code would be:
Get projectItems.Parent, which is an object, which in turn can be an EnvDTE.Project or an EnvDTE.ProjectItem.
If it is an EnvDTE.Project, use Project.ProjectItems.AddFromTemplate(..)
If it is an EnvDTE.ProjectItem, use ProjectItem.ProjectItems.AddFromTemplate(...)

How do I get the projects to build in the active Visual Studio configuration?

I am working on a small Visual Studio extension that acts on projects in a solution based on if they are set to build in the active build configuration or not. The problem I am having is that I cannot figure out how to determine what those projects are.
I have implemented IVsUpdateSolutionEvents, in which I implement OnActiveProjectCfgChange. I can get Visual Studio to enter the code block when I change configurations, and I have been able to get it to do many of the things that I would like to do, but without being able to determine what projects should be built in the active configuration, I am dead in the water.
My implementation so far is:
public int OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy)
{
var activeProjects = new HashSet<string>(); // TODO: Get projects in active configuration
foreach (Project project in _dte.Solution.Projects)
{
if (project.Kind != "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" // C#
&& project.Kind != "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}" // VB
&& project.Kind != "{13B7A3EE-4614-11D3-9BC7-00C04F79DE25}" // VSA
)
continue;
IVsHierarchy projectHierarchy;
_solutionService.GetProjectOfUniqueName(project.UniqueName, out projectHierarchy);
if (activeProjects.Contains(project.UniqueName))
{
// Project is to be built
}
else
{
// Project is not to be built
}
return VSConstants.S_OK;
}
}
What I need to do is figure out how to fill in the HashSet at the beginning of the function. (Marked with TODO). I have searched and searched, but I have not found what I need.
Does anybody have any references to documentation or sample code that might help me move forward?
You can use SolutionContext.ShouldBuild property
foreach (SolutionContext solutionContext in _applicationObject.Solution.SolutionBuild.ActiveConfiguration.SolutionContexts)
{
if (solutionContext.ShouldBuild)
activeProjects.Add(solutionContext.ProjectName);
}

Categories