How to create more Macros in C# Post build step - c#

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"

Related

How to embed the version information in the executable file name when building C# application in Visual Studio?

This question is a complement for the post How to change the output name of an executable built by Visual Studio.
After reading this post I did the following:
Firstly, I followed the answer for this post and I could define the executable file name successfully.
Now, I would like to know if instead of only define the name as "Demo.exe" as mentioned in the example post above, it would be possible to embed the version defined in AssemblyInformationalVersionAttribute or in AssemblyVersionAttribute in the built file, resulting in something like "Demo_v1.0.0.0.exe"?
I'm developing my application in C# WinForms, using Visual Studio Express 2017.
Why would you want to change the name of the executable? Whenever you try building a Setup for your application, you need to change the Setup to include the new file. And when you install an update, your Setup needs to know all versions of your executable in order to delete the old version. That's just not what you want to do.
If you want to keep all versions of the software for yourself, come up with a different solution, e.g. moving the executable into a folder which has the version number.
That said, I have done this for Setups, so customers can download different versions of the Setup. I did that using a commercial tool called Visual Build, but there are other build automation tools available. So, my answer is: set up a continuous integration / continuous delivery pipeline (CI/CD) and automate the step there, not in Visual Studio.
From the project properties, you can add Post build event command line to rename your exe
pseudo
Maybe you can create another console renamer.exe which reads version defined in AssemblyInformationalVersionAttribute or in AssemblyVersionAttribute of your app and renames it and then call that renamer.exe from Post build event command line
write a powershell script to rename the newly built exe and call that script from Post build event command line

Add environment macro to C# project

I want to run a utility (mt.exe) as a post-build step in a C# project. There is an environment variable/macro defined as DevEnvDir accessed by $(DevEnvDir). However, mt.exe is an SDK tool and not in the Visual Studio executable tree. Visual C++ projects have the WindowsSdkDir variable accessible by $(WindowsSdkDir). Ideally, I would want to have in my C# post build project a line that starts something like:
$(WindowsSdkDir)bin\x86\mt.exe .....
Is there anyway to add the WindowsSdkDir environment variable/macro to my C# environment?
I realize I could hard code the path, but I want something I can share between machines and developers.

Visual Studio user-defined property sheet macros in 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.

Compiling an executable file using Notepad++ and Csscript

I am very new to C# and am wanting to write my code using a text editor like Notepad++ and compile using csscript. I have the two working and I am getting results from my code.
However, so far, I have only been able to run my code as interpreted, but I will eventually want to compile exe or dll files.
Am I able to compile my code into a standalone exe or dll using notepad++ and csscript, please?
Just an update for your original question...
CS-Script plugin for Notepad++ actually allows building normal executables that can be executed as any other managed exe.
Little too late, but here's the one that worked for me: I called this batch script bnr.bat (Build and Run)
echo Building project..
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /out:"%1\%2.exe" %3
echo Running project
%1\%2.exe
pause
Save this bnr.bat file and and then use NPP's Run and set The Program to Run as follows and before you press Run button, press Save and provide your custom shortcut keys:
<directory_where_you_saved_bnr.bat>\bnr.bat $(CURRENT_DIRECTORY) $(NAME_PART) $(FULL_CURRENT_PATH)
the $ constants are defined internally in NPP:
$(CURRENT_DIRECTORY) is the full path of the directory containing your C# file.
$(NAME_PART) is the name of your C# file minus extension (.cs).
$(FULL_CURRENT_PATH) is the full path for your C# file.
This does not have any error checking, but pause in batch script will allow you to see the errors and exceptions within the console before you exit the script.
I had set the PATH environment variabile, but somehow this batch script did not find csc.exe, because it was looking at the npp bin directory.
For .NET 5+ (and .NET Core), you can compile your project using the .NET Command Line Interface (CLI)
The command
dotnet publish
creates the files you need to run your program.
No, you will need a compiler (Microsoft´s from VS or Mono)
csc.exe is what you need. It should be at C:\Windows\Microsoft.NET\Framework\v4.0.30319.
Here is a link to a reference. http://msdn.microsoft.com/en-us/library/2fdbz5xd.aspx
And check out Visual Studio express, it makes life easier.
Thanks for the reply.
I have Visual studio but the license expires in 7-days.
I am using Notepad++ with an add-in called cs-script. The add-in checks and runs code in a similar manner to Visual studio but it will not compile an exe or dll file.
However, to answer my own question and as suggested by quarksoup, the answer lies within the csc comiler. By using the /flags, I am able to compile my programs from the command-line. I shall write a batch file that will do the work for me.
Regards

Fast Re-build C# app under Windows 7

Need to recompile my project (not large) under Windows 7 without setting up Visual Studio.
Is there any method to do that with minimum setup procedure. It's C# app, using System.Net.Sockets and some others from my other projects.
Should I set up whole VS to recompile just little code app? like batch build..
Or... is there any cloud/public servers with pre-installed different Operational Systems with Visual Studio, where I could upload my project and just re-build it under all platforms at once?
I use this batch script to compile my C# applications. Just pass in the solution name without the .sln extension
if exist %SYSTEMROOT%\Microsoft.NET\Framework\v3.5 set MSBUILDPATH=%SYSTEMROOT%\Microsoft.NET\Framework\v3.5
if exist %SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319 set MSBUILDPATH=%SYSTEMROOT%\Microsoft.NET\Framework\v4.0.30319
set MSBUILD=%MSBUILDPATH%\msbuild.exe
%MSBUILD% /nologo /m /p:BuildInParallel=true /p:Configuration=Release /p:Platform="Any CPU" "%1.sln"
Do you just want to use studio to compile without running the GUI? If so, you have at lot less work to do. Use devenv.exe.
http://msdn.microsoft.com/en-us/library/xee0c8y7%28v=vs.80%29.aspx
This should work for you:
csc /r:Reference1.DLL /r:Reference2.DLL /r:Reference3.DLL Main.cs...other cs
/r: pass refereences of your project
after, pass cs files separated by space
In order to run this in CMD o PowerShell, you may need to run *vcvars32.bat, which you can copy from machine where you have VS installed, or just, before inserting command, set complete path to csc.exe*
Regards.
With the .net Framework installed, you can just use msbuild or csc.exe.
Build project on commandline with msbuild.exe (comes with the framework).
No need to use devenv (which is vs) or csc (which requires you to specify dependencies etc.)

Categories