Get data from in compile time C# - c#

I've been trying to write C# code that retrieves data from a file in compilation-time, and not in run-time.
The thing is that I need to get a version of a file that exists in my machine,
but not in machines that would run this DLL. (The version can be changed tomorrow, so I want to able to retrieve that dynamically).
In run-time, those machines have no access to the file, and that's why I need to get this data before.
Anyone as an idea how to do so ?
Thanks.

The C# code that you are compiling will not be run during the compilation.
You should make a pre-build step that extracts the version information. That custom build step could in itself be a C# program if you want. Make that program output a small .cs file containing the version number of the dll and include that .cs file in the source for the main program.

If you want to do something before or after a project is built you can write a command line statement in pre-build event command line or in post-build command line in Build Events section of project properties.

Related

.NET project in a solution should generate an exe but only makes a setup file and shortcut

I've inherited an old .NET MVC C# website. Bundled into the solution is a 'SearchUpdater' program which seems to be some kind of service:
public partial class Service1 : ServiceBase {
It seems to be an application, on properties we see:
Output type: Windows Application
Target framework: .NET Framework 4
This somehow translates to a SearchUpdater.exe file we have on the web server which is run every day. The code deletes a search index text file and then rebuilds it.
Simple, except I need to update the code logic and replace the .exe.
When I do this I can't seem to generate the .exe file any way I try. If I 'publish' the SearchUpdater project I get a local 'setup.exe' file, which, when I run it generates a kind of SearchUpdater 'shortcut' in my start menu which is a
Type of file: Application Reference (.appref-ms)
Location: C:\Users\Me\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\HP Inc
There's no 'target'...
Basically, I can't find a .exe file anywhere to replace the one on our webserver!
Any ideas? I'm a bit lost with this one, how can I generate a .exe, and will I need to do this somehow on the webserver? Am I doing something wrong?
Thanks to suggestions by #codecaster and #hans-passant. If you build the project in release mode, then go to the solution folder and check in the correct bin folder there is indeed a 'release' folder that will contain the .exe file (or did for me!).

How to merge a file to a precompiled exe application

I have designed and created two seperate windows application ( say winapp1 and winapp2). Winapp1 creates a file(say file.txt) in the disk containing some text. And I need to merge this file.txt with precompiled version of winapp2 ie winapp2.exe so that the winapp2 can display it.
Winapp1 creates a file with different contents each execution time. And for each file there will be a copy of winapp2 that needs to be merged.
Is there a way to this? If there is, help me.
And my English is not so good. Thanks for reading this.
The easiest solution, (assuming that releasing the source code for winapp2 to the system that runs winapp1 doesn't present problems) is to have the entire source for the winapp2 project accessible to winapp1. The source should include a blank copy of the file as an embedded resource. Winapp1 can then modify the source file and use MSBuild to build a fresh copy of Winapp2.
To modify the text file, you should be able to just use the normal file manipulation .NET methods and to call MSBuild, you may be able to do it via a .NET class, but you can certainly just call the msbuild executable from the relevant .NET framework folder in %windir%\Microsoft.NET\Framework\ and pass in the full path of the project file.

Can I require (the simpler way) to copy the resultant config-file of my dll instead of post-build event using?

My solution contains two projects. One of them is console application, but second is dll. Each of them has own output directory. Both projects contains App.config files. Their names in the output directories are MyConsoleApp.exe.config and MyLibrary.dll.config. Console application has the reference to the second project (its Copy Local property is True).
But compilation result of console application not contains the config-file of my dll in the console application output directory. Therefore I copy it through the post-build event of my console application.
Can I require (the simpler way) to copy the resultant config-file of my dll instead of post-build event using?
No, there is not. Application configuration files are not seen as a dependency of another project. Hence, they are not copied.
I am not sure if you really need that file, but if you do, you could keep your current post-build action. Another option would be to create it from your application if it doesn't exist and default the setting values from there.

Error copying files from one project to another

I've created a *.dll file in C++ to be used in a C# program. The native DLL resides in my managed DLL wrapper. From there, I physically copied my the native DLLs (NativeDll.dll for debug and release) into my C# console application's debug and release folders. When I run the C# program, it runs as expected creating objects and displaying values.
The problem comes when I changed a default value of a variable in my native library (C++). When I run the program again, the changes to the variable (the with of a box) still holds the last value. From what I've searched for, I've found that I could copy the dll from one project into my C# project by this means in the project's Build Events/Post-build event command line:
copy "$(SolutionDir)ManagedDll\bin\$(Configuration)\NativeDll.dll"
"$(SolutionDir)TestDLLConsole\bin\$(Configuration)"
When I run the program again, I get an error stating "exited with code 9009."
The only way around this is to manually re-copy my NativeDll.dll files and overwrite the existing one whenever I make changes.
The 4 projects I have in my solution are with next build depending on the previous:
NativeLib (static library)
NativeDll
ManagedDll
TestDLLConsole (where I test to see if the dll works).
How do I get the Dlls to be copied into/overwritten every TestDLLConsole is compiled?
Maybe you are trying to overwrite the last dll that you copy with the last post build event. You can try:
copy /Y "$(SolutionDir)ManagedDll\bin\$(Configuration)\NativeDll.dll"
"$(SolutionDir)TestDLLConsole\bin\$(Configuration)"
The /Y parameter suppresses prompting to confirm you want to overwrite an existing destination file.

How to add source file (.cs) to resources (.resx) in a pre-build event?

Is it simple or even possible?
Why: Following my other question and using this approach, now I would like to simply embed the source file to the resources so I can copy it to the temp folder while the application is running - and keep the application as a single file.
I assume using a pre-build event is the best way to do this. If so, how would it be done?
EDIT: I'm asking about adding it before building because I also assume it won't be automatically updated if I add it manually once and then change the code after.
I recently went through this same issue when developing an examples suite for a .NET control which had to display its own example source code in the application.
The approach I ended up with was as follows.
I created a batch script (run in pre-build) to copy all the code files under /Examples/* to another folder in the solution Resources/ExamplesSrc/*
I then included all these files under Resources/ExamplesSrc/* in the csproj and saved it.
The next step was I set all the files under Resources/ExamplesSrc/* as embedded resource and committed the change to SVN. I did not commit the examples (generated) to SVN, just the modified csproj and batch file.
The effect when developers checked out was that they had (!) icons on all the resources (they were not on disk, but the csproj was looking for them), however as soon as they built the generated files appeared and all was well. This approach also worked with a build server (Team city) and the examples suite can now load and browse its own code via embedded resources.
One caveat you should be aware of. There is a quirk (by design apparently) in VS2010. If an embedded resource has ".cs" in it (i.e. any code file!) it cannot be loaded from the assembly using Assembly.GetManifestResourceStream(). To work around this all source files were renamed in the batch copy step from *.Xaml.cs to *.Xaml.c.txt, from *.cs to *.c.txt
Hope this helps!
Turns out I don't need to (re)add the source file each time I build the solution!
When I add it manually once it becomes a "Text" file on resources - and I can easily access it as a string using Properties.Resources.SourceCode (having the file named "SourceCode.cs"). And yes, it is updated automatically since the resource property "Persistence": the file is linked at compile time.

Categories

Resources