Add environment macro to C# project - c#

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.

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

How to explain the differences in these nested Visual Studio projects?

I am new to .Net and Visual Studio. In order to learn more, I decided to create a simple asp.Net application in my spare time. In Visual Studio I am trying to follow the same project structure as the application I support at work. I noticed that my work application has the following project structure
Development
|_services
|_RedSun.Onvia
|_various VS project folders (i.e. RedSun.Onvia.Core, RedSun.Onvia.Web, etc)
|_RedSun.Onvia.sln
|_RedSun.Onvia.v12.suo
Notice how the VS solution files are on the same level as the RedSun.Onvia solution folder. However when I create a basic empty project Visual Studio, it gives me the following structure...
Development
|_services
|_RedSun.Onvia
|_RedSun.Onvia.sln
|_RedSun.Onvia.v12.suo
Note the RedSun.Onvia.sln and RedSun.Onvia.v12.suo files are a directory deeper than the solution folder. When I tried moving files around manually with File Explorer to try and match the structure above I was unable to open the project in VS due to errors.
Can anyone please explain how I can get the same structure as shown in the first example?

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"

Does Visual Studio conceal some details of execution environment of c#?

I am very new to c#.I started c# today.I don't know execution environment of c# very well.
I can explain my question more by taking netbeans as example.
When we create JSP project in Netbeans then it creates web.xml by itself.
This file is needed to deploy the project.So if we want to create JSP program without using netbeans then we have to create web.xml by ourself.
So my question is does VS also create files by itself that are very neccessary to run C# program?
Thanks
Save to a hello.cs file:
class P { static void Main() { System.Console.WriteLine("Hello World"); } }
then from a command prompt in the folder of hello.cs do:
%Frameworkdir%\v4.0.30319\csc.exe hello.cs
If you haven't done errors, you have compiled your first program. You don't need Visual Studio to do it.
So, yes, Visual Studio clearly generates all the files needed to compile a simple program (even a complex one, if you direct it correctly), considering that a .cs file is in truth all you need.
What Visual Studio 2012 generates for a console program is:
a solution and a project file (that are used to organize your source code and remember references to libraries)
a configuration file, (not necessary, but Visual Studio 2012 adds it just to write that the program will run on .NET 4.5)
your source code program
another source code program (AssemblyInfo.cs) so you can add copyright/other metainformations to your exe.
some random files that Visual Studio uses as cache (the .suo files)
I assume you are speaking only of ASP.NET C# execution, since you mention web.xml for Java. There is a web.config that is probably very comparable to that, since I am not a Java developer I really do not know.
The web.config is actually XML and it contains things like connection string to the database, HTTP modules, HTTP handlers, custom application settings that go above and beyond what is part of the core .NET Framework.
The difference lies in that this is not a unit of deployment, like it sounds the web.xml is in Java, but rather web.config is a run-time explanation of settings that the web server (IIS) uses properly run the application.

How to add source code to .dll file in Visual Studio Express 2012?

I have a 3rd party .dll file and its source code as well ( written in C# ). I can create a new project and add reference of this .dll file to use its methods. But when I debug I want to step in to the methods defined in .dll file ( Just like we do when we have .jar file and its source-code in java ).
How can I do this in Visual Studio Express ( I have VS 2012 ).
The easiest, if you have the source code, would be to add the project to your solution and compile both at the same time. You could then breakpoint everywhere! (And don't you dare tell me breakpoint isn't a verb)

Categories