Visual Studio user-defined property sheet macros in C# - c#

I have inherited a C# solution where the projects have a configured "Post-build command line". In this command line, there are a couple of user-defined property sheet macros that copy various output files to specific folders. However, when I build any of the projects, the macros are incorrectly defined as empty strings.
E.g.
copy "$(TargetPath)" "$(PluginPath)\$(ConfigurationName)"
The standard macros, e.g. $(TargetPath), work great, but I can't see any way of controlling the value of the user-defined macros. In the post-build step there is a "Macros >>" button which shows the standard macros, but there's no way that I can see to either to edit their values or add new, user defined macros.
It looks like the previous developer had this working, so what am I missing?
I have read that macros can be defined in .vsprops files, but only Visual C++ projects support these files. When I look in the Property Manager window, I see only the message "No Visual C++ project is loaded". (I'd expect that user-defined property sheet macros would be equally as useful in the "Post-build command line" of C++ projects as they are in C# or projects in any language.)

With a quick search I found this, it might help.
Update:
After adding the following to my .csproj project file, I can use the PluginPath as a macro in the post-build command line with copy "$(TargetPath)" "$(PluginPath)\$(ConfigurationName)" (tested in Visual Studio 2008)
...
<PluginPath>C:\apps\</PluginPath>
</PropertyGroup>

You can follow any solution as described below.
Create batch file and insert following code
set MY_INCLUDES_DIR=D:\MyIncludes
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"
"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" D:\MySolution.sln
Start Visual studio solution by double clicking this file. Advantage of this solution is all projects can use same environment variable.
Define following xml tag in visual studio project file.
<PropertyGroup>
<MY_INCLUDES_DIR>D:\MyIncludes\</MY_INCLUDES_DIR>
</PropertyGroup>
Create system environment variable named MY_INCLUDES_DIR and simply use $(MY_INCLUDES_DIR) in visual studio.

Related

$(TargetDir) replacement for SDK-style projects (analogue to "$(MSBuildProjectDirectory)\" for "$(ProjectDir)")

I have an old project in non-SDK-style format generated by Visual Studio. It contains a post-build event in which $(ProjectDir) and $(TargetDir) are used.
I am currently transforming the project into a new SDK-style project (csproj file).
If I just turn that post build event into a Target Exec Command, I get errors. After prefixing the command with echo I could see why: The values $(ProjectDir) and $(TargetDir) were both empty.
I figured out that replacing $(ProjectDir) by $(MSBuildProjectDirectory)\ gives the desired result, but what is the equivalent for $(TargetDir)?
Note that I am not using Visual Studio for the new SDK-style project. I am using Visual Studio Code. It seems that Visual Studio sets values for $(ProjectDir)/$(TargetDir)/others, but Visual Studio Code does not. I build my project using dotnet build.
Seems I found the answer here:
The value of TargetDir is computed as the full absolute path of whatever you specify as the Output Path for your project, which is stored in the OutputPath property directly in your project file (.CSPROJ, .VBPROJ, etc.).
So I believe $(TargetDir)would be $(MSBuildProjectDirectory)\$(OutputPath).

Visual studio doesn't trigger MSBuild

I've created my own targets file which during the build process runs a custom tool generates some C# code which are then included in the .csproj.
I have something very like this.
<Project Sdk="Microsoft.NET.Sdk" InitialTargets="MyTarget">
.....
<Import Project="MyTargetsFile.targets" />
<ItemGroup>
<XYZ Include="**\*.xyz" />
</ItemGroup>
</Project>
The problem I encounter if that if I change the .xyz visual studio does trigger a rebuild because it considers it up to date. On the other hand if i run msbuild Systems.csproj /v:diag > Build.log it detects the changes and it rebuilds the project.
After a bit more research I've reached the conclusion that VS doesn't not even trigger MSBuild in this case. Is seems to me that visual studio just checks just .cs files for changes and ignores the rest.
Now my question is how do I make VS trigger msbuild if I've made a change in my .xyz file?
I'm using Visual Studio Enterprise 2017 Version 15.6.6
Update
I know I can set the build action for the files to Compile but they contain .xml and that causes an error.
I also know that I can clean the project and build, but I'm interested in an incremental build not a full one.
After a lot of googling I've been able to find a solution, not necessarily the best, but it seems to do the job.
As stated in the question my problem was the vs was not triggering msbuild. Looks like in order to let msbuild always run (this does not mean that you get a full build eveytime, it just means you let msbuild decide what to do and not vs) you have to add
<PropertyGroup>
<DisableFastUpToDateCheck>True</DisableFastUpToDateCheck>
</PropertyGroup>
MSDN says the following about DisableFastUpToDateCheck:
A boolean value that applies to Visual Studio only. The Visual Studio
build manager uses a process called FastUpToDateCheck to determine
whether a project must be rebuilt to be up to date. This process is
faster than using MSBuild to determine this. Setting the
DisableFastUpToDateCheck property to true lets you bypass the Visual
Studio build manager and force it to use MSBuild to determine whether
the project is up to date.
Another way to solve it although I'm not sure it's suitable for everyone, it to set the build action to Embedded resource.
Visual studio doesn't trigger MSBuild
Do you mean Visual studio doesn't trigger MSBuild if you change the .xyz?
It depends on the Build Action of your .xyz file. If you set the Build Action of that file to C# compiler, when you change the content of that file, VS/MSBuild will compile this file to check the content of this file. If it was changed, it will considers it not up to date, then trigger MSBuild.
The default build action of .cs file is C# compiler, most the default build action of the rest files should be None. When we add a file with build action None, VS/MSBuild will not check the content of this file, only check the input and the output of this file. That the reason why Visual studio doesn't trigger MSBuild if you change the .xyz.
For example, when we add a text file in the Visual Studio with build action None, build the project. Then I change some content in the text file, build again the project, it doesn't trigger MSBuild.
If we want to the MSBuild was triggered after we change the text file, we recommend to clean the build to remove the output, then when we build it again, MSBuild will be triggered.
Then I change the build action of the file to C# compiler(To avoid introducing compilation errors, I use space instead of code in this file.), I change the text file with a space after this project is up to date, build the project again, Visual studio trigger MSBuild.
Hope this helps.

How to create more Macros in C# Post build step

I have an Visual Studio 2012 Solution that includes both C# and C++ projects.
I want to create a postbuild step and I notice that the C++ projects have much more macros than the C# projects. I need to get the WindowsSDKDir which is available in the C++ macros but not in the C# macros.
C++ Macros
C# Macros
Can I see all the Macros that are available to the C++ projects in the C# projects??
I can think about the following workaround. Write this post-build command:
$(ProjectDir)post_build.bat
Create post_build.bat file in the project directory and fill it by this way:
echo %WindowsSdkDir%
echo %WinDir%
Build the project, it prints:
ECHO is on.
C:\Windows
So, C# is not smart enough to see Visual Studio build environment variables (%WindowsSdkDir% is expanded to nothing), but at least general environment variable like WinDir is expanded. So, you can create your own environment variable with the same value as WindowsSDKDir and use it in batch file called from Post-Build step.
To make something more useful then echo, create batch file with parameters (%1%, %2% etc.) and call it from post-build step, passing required parameters. For example:
$(ProjectDir)post_build.bat $(TargetPath)
In the batch file %1% will be expanded as output file name.
Simply call the batch file that sets up environment variables for a Visual Studio command prompt as the first line of your build event. This will set up all the environment variables for use in later lines of your build event. For example...
call "$(DevEnvDir)..\..\VC\vcvarsall.bat"
"%WindowsSdkDir%bin\x86\rc.exe" <parameters>
Note that the environment variables are Windows environment variables and need to be accessed via the %VAR_NAME% syntax rather than the $(VAR_NAME) syntax.
The location of the vcvarsall.bat file is consistent over at least VS2012, 2013 and 2015, meaning that this solution is generic and doesn't need tweaking for different VS versions.
If you use msbuild you will find that $(DevEnvDir) is not set correctly when building in msbuild. In that case, this alternative should work for both msbuild and visual studio build providing you have not customized the visual studio install location.
call "%ProgramFiles(x86)%\Microsoft Visual Studio $(VisualStudioVersion)\VC\vcvarsall.bat"

Attach VsPackage to Roslyn Instance

I'm trying to create a VsPackage that makes use of the Roslyn Language Services. Under the properties of my VsPackage, I've changed the command line arguments to:
/rootsuffix Roslyn
When running the project, the instance of Visual Studio that starts up is correctly using Roslyn. (I see [Roslyn] next to the names of .cs files I have open). However, my VsPackage is not deployed to this instance of Visual Studio.
I have opened up the SyntaxVisualizerExtension VsPackage that ships with Roslyn and compiled/run that. It correctly deploys to Visual Studio using Roslyn. I've looked through the project properties and references and nothing has stood out to me as missing.
The steps I'm taking are :
File > New Project > Visual Studio Package
Create with Tool Window
Open Project properties, change /rootsuffix Exp to /rootsuffix Roslyn
Add references to Roslyn .dlls
Run project.
In theory, I could probably strip down the SyntaxVisualizerExtension Package, but I'd like to know the proper way to create a VsPackage for the Roslyn instance.
There's no UI setting to control what the property is. Edit your project file in Notepad (or unload the project in VS and then right click and choose "edit"), and add this line:
<VSSDKTargetPlatformRegRootSuffix>Roslyn</VSSDKTargetPlatformRegRootSuffix>
into the appropriate PropertyGroup. Look in the projects created via any of the Roslyn templates for an example.

Multiple icons in executable in VS2010

I have Googled and found multiple ways of adding multiple icons into the executable, but they all seem to work for VS 2003-2005-2008, nothing for VS2010. I have not tried the Win32 resource with /win32res because I do not know how to use it (can't figure to get a good Google result for that either).
Any simple suggestion?
I've just created a simple tool to do exactly this without having to mess with .res files. It's a tiny utility which you can use as part of your Post-Build event and lets you add all icons files in a particular folder to your assembly. If we assume that you have a icons folder under your main project folder you can add the following post-build event:
C:\path\to\InsertIcons.exe $(TargetPath) $(ProjectDir)icons
A further description and a download can be found at http://einaregilsson.com/add-multiple-icons-to-a-dotnet-application/
This works for me:
http://www.codeproject.com/Tips/160885/How-to-Embed-Multiple-Icons-and-Color-Animated-Cur.aspx
If you're using visual studio 2012:
For C#.NET Here I found a good solution for this problem for c# projects as an example. But it only works in my C# projects
Create a new "Native Resource Template" from the File | New dialog box.
In project properties(project->application->resources) there is option to choose resource file (.res) rather than "Icon and manifest" which is selected by default (This option is visible only to C# projects!).
For VB.Net projects this link (Also mentioned here by Waldo) can be more helpful because in my visual 2012 there is no option to select/browse Native Resource Template(.res) files but you could manually change project definition file for vb.net project as described to compile project win a native win32 resource file:
Open your project file in notepad (*.vbProj) and add the following block:
<PropertyGroup>
<Win32Resource>assemblyWin32.res</Win32Resource>
</PropertyGroup>
The Code Project article explains how to create a "assemblyWin32.res" file.
https://www.codeproject.com/Tips/160885/%2fTips%2f160885%2fHow-to-Embed-Multiple-Icons-and-Color-Animated-Cur

Categories