C# Console application keeps creating a Configuration FIle - c#

I am new to C#. I am an admin, not a programmer, but I am starting to see how being able to write some console apps could help me with automation. So I wrote one and it works well.
Here is the issue. Every time I execute the exe file for the console application, it generates a .config file. There isn't really anything in the config file. just a few lines. How do I make it stop. The Program.cs is really just calling SCOM's SDK to put a machine into maintenance mode.
Developed in: c# in visual studio 2013
.net version used - 3.5

It's not entirely clear what you're asking. But assuming commenter Jon's guess is correct, and that you really mean that when you build your executable, a .config file is generated, then his advice is correct: just remove the "App.config" file from your project, and no .config file will be generated. The "App.config" file is the template for the .config file; at build time, the file is copied to the output directly, using the executable name and the extension .config.
Please note that this may or may not be safe to do. It probably is. However, some C# projects have configuration information in the .config file that is actually needed. Please see Is app.config required in .Net 4.0 C# projects? for more details on that question.
Finally, I will suggest that for simple automation tasks, you may find other tools more appropriate that even a simple C# console program. The two I use most commonly are PowerShell and plain old batch files, though there are a number of other similar scripting-style automation tools that work on Windows (including a variety of *nix-style shells that are compiled for Windows).

Related

How to change variables of a .exe file?

I want my program in Visual C# to allow the user to generate a portable .exe file to be run on another user's computer, with the portabe .exe receiving the values the first user inserted. I don`t want to use a configuration file or any other file to store the input values to be then read because I want the .exe to be a single file. I have the main and the second program (future portable executable)
as different projects on the same solution. Is there a way to modify values of the portable .exe or does a new program needs to be compiled on the fly?
To formally answer the question.
You can convert the assembly to IL, search and replace and recompile. However, if you are building this application, you should really consider the need and use case for such very thoroughly.
If you need just to modify the behavior of an application, you have a plethora of options.
Registry (can be remotely done)
A config file (though you ruled this out)
You could use a login method to a resouce
Wcf for instance
Active Directory
A network DB
Rebuild you app for these users
Msbuild with a batch file to tweak something
A pre build script
If you are really tricky you "might" be able to track down the offset of an int and tweak it. However this seems very unreliable
Or if you really have your heart set on this, see the following and associated questions
Modify Emdeded String in C# compiled exe
Is it possible to Add/Remove/Change an embedded resource in .NET DLL?
Update from Ben Voigt Comments
Win32 Resources can be changed after being embedded in an EXE. .NET
System.Resources can only be changed before embedding. C# applications
are compatible with both kinds, but the distinction is very important
The The necessary function is UpdateResource
You can typically append to an .exe without breaking it and then read the data in upon execution.
See: appending data to an exe

Creating a "portable" .exe (without installer)

I've recently coded a little program to determine numbers in a picture and it is reliant on two libraries I've used. (DLLs)
Since my target computer is not allowed to install programs due to security reasons, I need to create a portable .exe.
.NET is installed on the target computer but for some reason VS still does not include the libraries I've used in the exe but instead creates an application folder with a setup.exe, some .DEPLOY files and an application manifest.
I am new to VS and .NET in general so this question could be easy to answer, but I'm asking since I've found nothing useful on StackOverflow neither on google.
You can simply build the application and copy your bin/Debug folder along, but that would still mean you need multiple files.
In order to merge all references into the executable, use ILMerge. Here is some help calling ILMerge.
Basically, after building, you should do something like this:
ilmerge /target:winexe /out:SelfContainedProgram.exe
Program.exe ClassLibrary1.dll ClassLibrary2.dll
There is just one file you need to send along.
One way to do this is to build your application in Release mode (You can pick from Debug or Release in the drop-down). Then go to C:\Projects\[ProjectName]\[ProjectName]\bin\Release (The location of your project folder may vary). You'll see a bunch of files but all you really need are the DLLs, executable, and the config if you used one. You won't have to do any setup if you keep the necessary files in the application's folder, just copy them all to a folder on the target computer, create a shortcut if you want then you're good to go.
You can just copy all your assemblies into any folder you want. Simply chose "Build" from within Visual Studio and copy the files from bin/debug to your destination-folder.
However you have to ensure that all (relative) paths (if existing) still work as you cannot be sure where the user of your program copies the files to.
One simple way could be to use 7zip Packager, it doesn't need any installer. However, VisualStudio method might be more reliable.
I encountered the same issue recently. ILMerge suggestion above is no longer supported. I found Fody.Costura as a modern replacement.

Copying and renaming resource files with script

I'm currently working on a project with multiple language options. I have loads of resource files with my first language and want to copy these files and add some text to them using some sort of batch script in visual studio.
From within VS it will be hard to achieve. There are many standalone tools to edit .resx files separately (like http://resx.sourceforge.net or http://resxmanager.com). Great help when translation gthe files, but I do not know any capable of scripting/batching the job.
RESX are simple XML files, so I think you'd be quicker to write simple console app to load the XMLs via standard C#/Perl/Ruby/Python libraries, patch the texts, and write them back to files instead of searching for a scriptable tool, but maybe I'm wrong. I'd be happy to see such tool too! :)
-- edit:
With pure .bat batch files, it may be hard, because the Windows Console simply lacks proper string-replace commands and you have to use some tricky commands like see the BAT here.. At least small tool like grep or sed would be handy, and the script would be 100% more flexible.
You can rather easily do it with PowerShell, CygWin, Ruby, etc or even the JScript/VBScript that you should already have installed along with your Windows. See here for a such a script in VBS. It scans only a specific directly witout subfolders, but you can easily mix it with this sample to get recursive directory walking. Btw. VBS is kept in files of such extension: myscript.vbs and may be simply double-clicked or simply run just like .bat or .exe files.
All of this however are more like typical programming.. If you have some complex renames to do and if you dont have any Python/Ruby/blargh at hand, I'd recommend just writing it in C# as the code will be friendlier.

Release Windows Forms project without an installer

I've built against the release profile and this creates an executable build within the release directory in my project.
How do I best distribute to clients from this executable? There are a lot of files within this folder which don't appear when installed through the installer, such as the mainifest and one called application.exe.xml (which is confusing when Windows hides the extension in Windows Explorer).
Are these all necessary? Can I just send the executable? Or will I need to send with all the files? Is there a way to build without all these files?
You must send the EXE file and any DLL file that you reference locally. If you use COM references and the like, you need to register them during the installation. The same thing for the GAC I think, but I haven't used that for stand-alone applications myself.
The application.exe.config file contains the application settings (a copy of app.config). If you don't use settings or the user doesn't typically care about them, you can omit the file, and it will use the default values you built the application with.
The vshost files are not needed (if you have them). They are used by Visual Studio's debugger. The .pdb files contain debug data used to facilitate DLL file to source matching. Unless you plan on attaching a debugger to the application, there is no point sending those.
Usually, in an XCopy deployment you have to deploy (literally copy all files deployment with no setup/installer program) the content of the output folder (like debug or release) without:
*.pdb - debug symbols
*.xml - xml documentation
?vshost? - Visual Studio hosting files
In fact, it anyway also depends on your specific application. As a developer, you need to know what you are producing; in case you are using an xml file which is not the result of the .NET documentation compiler, but a static file copied in the output folder, then do not forget to deploy it.
A last note: developers usually disable the option to hide file extensions in Windows Explorer ;-)
You need to understand what an installer does or why an installer is important.
An installer takes care of the basic environment. The installer can carry dependent assemblies/modules along with the application. It can also check if you need something before you run, like .NET on the target machine. It can also create the shortcuts on the desktop or start menu. Plus it also provides adequate options on the target machine to uninstall it.
If you wish to ship the executable alone, you might miss out some assemblies that the executable depends on. The target machine may or may not have the correct .NET version installed.
Use the program: HM NIS EDIT from HM Soft.
Build your project
Run NIS EDIT
Make a new script from the wizard (Ctrl + W)
Run all the steps
Select all the .dll and .exe files
Build a setup file

How to create a single executable file in Visual Studio 2008?

I've created a Windows Forms Application in Visual Studio 2008, .NET 3.5.
Now I want to finalize my project, i.e. to create a single .exe file which I can give to someone and he will be able to run it on his computer.
In my project files I found bin/Debug directory where I see a .exe file.
Can I just use this file as is, or I am missing some important finalizing procedure ?
Thanks !
You can just take the EXE as long as it has no dependencies. If there are any dll files in there, you need to include them (or use the ILMerge command line utility to combine them). You can, however, ignore any generated pdb or xml files.
As a side note, you should be compiling in release if you plan on deploying your project (the output will be in bin/Release)
You are not clear in you question. If your solutions output is just a single exe file, then Yes, you can give away the exe file in bin\debug.
But instead of bin\debug\ exe you must distribute the bin\Release\ exe which is meant for release purpose.
To make the ILMerge easier, use the GUI tool from codeplex at http://ilmergegui.codeplex.com/
If this is a commercial application, several commercial obfuscators allow creating a single exe.
In SetiSeeker response the link http://ilmergegui.codeplex.com/ is not reachable

Categories