Generating a file during "Build", c#, Visual Studio - c#

How can I generate a file during "Build" in Visual Studio using C#?
I want to create a .txt File and add some text in it. The file should be created directly when I press Build and save it in a place where the Release or Debug Folder is.

You should have a look at build events.
Typically you can run any kind of script pre and post build - including the (re)generation of a text file.
To copy files into the output path, you can use the OutputPath variable. See the MACRO section and this list on how to use them.
As by #Oliver's comment: if it is a static file, you can just include it in the project using its properties and select: Copy if newer.
Taken from the documentation:
And a subset of the MACROs

Related

Run T4 Text Template when click update button or application restart

In my application, some setting are coming from JSON settings file. The setting file can be updated from within the program and a .cs file is created from these updates with using T4 Text Template. So when the JSON file is updated, I want that the T4 Text Template also works and update the .cs file. If this is not possible, I would like the T4 Text Template to run and also update the .cs file when I restart the program. But I have no idea how to do it. I managed to create .tt file and it works perfectly when I save .tt file in the Visual Studio.
Note: I have googled for 2 days and read all topic in the site. But I did not reach any result.
You can run command-line transform tool. Here’s an example:
"C:\Program Files (x86)\Common Files\microsoft shared\TextTemplating\14.0\TextTransform.exe" "C:\src\template.tt"
That will use TextTransform.exe from VS2015. VS2017 also has TextTransform.exe, even the freeware community edition, it's located in C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\
Documentation for the CLI is available.

Where does visual studio setup project get the primary output file?

I am trying to create a setup project for my C#-application in VS 2010. I am using an post built event during the creation of the c#-application. This event appends data to the binary of the application, because i need this data later.
The Problem is, that the file in the output-folder contains this appended data but the installed file of the setupproject doesnt contains it. My Question now is, where does VS get the output file for the setup project, so I can append the data to this file?
Try adding "File" instead of "Project output" to your setup project.
Or you can just modify your output in /obj/release (or obj/debug) folder instead modifying it in output folder. (if you look on primary output properties - you will see that it uses files from obj... folder and not from output folder)

Generate c# projects in code at runtime

I am developing a development C# Generator and I need to make a class, but I don't understand how I can make the .csproj files.
These are just text files.
cs proj is an msbuild file. And cs file is just a nomale code file.
The best thing to do is to use a sample .csproj and .cs file and use search and replace to put the correct values into it. This will get you started.
When you understand the theory you can move on to a better templating engine such as RAZOR or T4. And then onto the better techniques of generating assemblies at run time.
Create an empty solution and save it in a location. Then add a project to that solution and save the solution to a different place. Open these two solutions with a text editor and compare the files, so you can see how does the solution file change if you add a project to a solution. In the same way, if you want to add a project to that solution programatically just read the solution file using textreader edit the solution file text and write it back to the same solution file.
Hope it helps.
Try the following batch code, which requires the dotnet command to be available.
This generates your .cs program, .csproj file, bin folder and obj folder.
cd Projects
set /p user_in=Enter the new folder name:
dotnet new console -o "%user_in%"
cd "%user_in%"
dotnet restore

Is it possible to "collapse" content file paths in build output in visual studio?

I'm using an open source library (log4net) that comes with LICENSE, NOTICE, and README files that are supposed to be included when you distribute it. So I want to make visual studio put them in the output directory along with my binaries.
I have log4net and its three files in a "lib\log4net" dir. The problem is if I add the files as content files, they get copied to bin\release\lib\log4net, not bin\release. Is there any way to make visual studio collapse the paths when building?
I originally used a post-build step to copy them, but then visual studio isn't aware of them and it won't put them in any other dependent project's output folder (which it will do if you call them content).
You could use a post-build step on your project and just move them to the directory you want them in. I frequently use pre-build steps to bring in the latest versions of dependencies and post-build steps to arrange my project the way I want to deploy it.
If you are using an install project to package everything, what about adding the extra files directly to that instead?
All you have to do is right click the file, README, for example, and go to properties. One of the properties is "Copy to output directory". Set it to true.

Adding XML Files to the Build

I'm using Visual Studio C# Express and I'm wondering how I would go about adding some XML files and being able to reference them in my code. I added the XML files in a folder under the project, but I'm not sure how I can reference them, and then get them copied to the output folder. Originally, before I added them, I just copied the XML files to the Debug folder for Visual Studio, but then when I compiled/installed a new copy of the program I had coded, I had to manually copy the XML files over.
Is there a way to add XML files to a Visual Studio Project and be able to reference them in the code and then have them copied to the output folder?
Right click project, Add Existing Resource, browse and select the file you want to add. Then right click the file and click properties and change "Build Action" to content, and "Copy To Output Directory" to Copy if newer (or copy always if the need be). Then you can access it by using the relative path.
I use this for my XML and I can access my content using the following code:
XmlDocument document = new XmlDocument();
document.Load("Resources/DefaultConfig.xml");
Please note that my DefaultConfig.xml file is inside a "Resoruces" Directory which I created in Visual Studio(this is optional, but it helps me keep my project neat)

Categories

Resources