I have a command line utility app designed for making server backups. I've also got a GUI app that gives access to various maintenance functions, including the ability to make server backups. Previously, this app duplicated the backup code, which was causing consistency and maintainability issues. I'm in the process of refactoring the GUI app to reference the command line app and call its backup methods instead, in the name of code reuse.
I've just run into a problem, though - the command line app has a config file, read using ConfigurationManager.AppSettings. When it's called directly, that works fine. When it's called as a reference from the GUI app, though, it tries to read the GUI app's config file instead, as the executing assembly, and throws an error when it can't find the values it expects.
How can I force the code to always read the command line app's config file, even when it's not actually the executing assembly?
Just set AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
Related
I'm trying to figure out how to inform the user that the app crashed because of a missing config file. I could use
if (!File.Exists("application.exe.config"))
{
CreateLaunchErrorLog("Message....");
Environment.Exit(0);
}
but the application crashes before the Main method. Any way around to get this to work?
The config file is intended to be modified by the user.
If the application is crashing prior to main because of the missing config file, you might want to rethink how you are structuring your program.
For example, you may want to write a function that checks for the config and then loads all of its values, as the first function called from main or in a setup routine when the program loads.
This gives you the added benefit that besides displaying a message, you could recreate the file with reasonable defaults and then continue on with the rest of the program.
These answers might help too.
The application should not crash even if the configuration file does not exists unless the file is being used through code. Are you sure the config file or any app setting is not being called before?
The application is a form application and it is too complicated. It is mostly used to connect to a database with an user interface. It also uses third party dlls.
I copied my VS C# application`s bin folder to my desktop.
"C:\Users\asd\Desktop\bin"
After that, I made some changes in my solution and building it, I again copied the application`s bin file under C:\;
"C:\bin"
Now when I run "C:\Users\asd\Desktop\bin\Debug\LIVE.exe" and let it remain open I can`t run "C:\bin\Debug\LIVE.exe". There is no error when I try to open the second .exe file. It simply does nothing.
I want both of the applications to be open at the same time.
Depending on the application, there are ways to prevent execution of a second instance (I have actually implemented the described behaviour in production code) so check the production specs of the application to see if that's a desired behaviour on client machines.
Because it's using a database connection and third party DLLs, there may be other specific limitations in place preventing proper execution, so check whether any exceptions are being cuaght by hooking into the FirstChanceException (WARNING: Never use this code outside a debug context!)
#if DEBUG
static Program()
{
AppDomain.CurrentDomain.FirstChanceException += (sender, e) => { };
}
#endif
Insert this into your Program class, or whichever class houses the Main method (and rename it if it's not Program of course) and then breakpoint the opening of the handler - this will often catch lots of exceptions you're better off not worrying about, but it may also clue you in if there is a problem keeping your code from starting.
As always, make sure to run this from within VS's Debug, so as to see any exceptions as they occur.
I need to create a library file which is to be placed with the bin folder of pre-compiled file.
So when ever any database connection is opened in that pre-compiled file the database connection must come to that library file and follow certain rules of mine and return it.
For example : Let us consider a web project which is deployed using publish website.Now I need to place my own dll in to that bin folder there by when ever the connection is opened in that project that connection string which is passing must read by my library file i.e. there by I can change some values in connection string(i.e database name etc....)
Is it possible to do so?
So you want to place a .dll into a bin-folder for a project, then make that dll "pick up" any connection to a DB that is made inside the project, is that correct? Then you want to be able to manipulate the connection string that is used?
Is it possible for you to edit the main project too? I.e. make the main project call and make use of the .dll-file? If so, you could just encapsulate the whole connection-logic into the .dll. Otherwise, this sounds quite difficult.
If you can not change anything in the main project, you would at least need a wholly separate program to sit and "listen" for connections. You would then need some way to intercept the connections from your main project, but even then, getting the connection-string, manipulating it, and changing the way the main project uses it sounds... challenging.
What are you actually trying to do here anyway? I would recommend trying to either place all DB-connection stuff in the separate .dll, and using it as the library it is, OR look for some other form of config-solution to solve your problem (ie provide different connection-strings in a common config-file for use in different situations...?)
I'm a Silverlight/ASP.NET developer trying to write my first Windows Forms application to run in the background on a server, populating our database. Eventually would like this to be a Windows service, but it's not required initially.
I need to create a batch file to execute 5 instances of this application, passing in the URL to 5 RESTful endpoints. So I published my app, which created a setup.exe. After installing it, I have an item that points to
C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\, with a .appref-ms file.
I'm not sure at this point what to do. Running:
"C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\StreamingApp.appref-ms" -"http://www.myURL.com" throws up a command window briefly, but the app doesn't run, data doesn't populate in DB.
What am I missing here?
since your application is in .exe format. And make your winform accepts command line arguments (check the main method) also make your Form ctor accepts params too. Then just launch it via cmd line just as you would other command, but here only to navigate to that dir where file exists.
In case of batch, use start command followed by program name and then arguments
It's hard to understand what is happening inside your application. You need to debug to understand what is going on there when it receives given parameters.
So I would suggest to debug an EXE. For this go to your EXE project properties, select DEBUG tab in CommandLineArguments insert your parameter string.
Run it in DEBUG and hopefully you will figure out a problem.
If after debugging it's not yet clear why it behaves in that way, come back to SO :)
Silvi if you plan to use your windows forms application from a batch file and you imagine the applicationm will behave differently in such mode than when opened witha double click, the usual approach is to parse the command line (arguments, also available in the main method as parameter) and to avoid loading the UI at all.
in fact if you have written your application properly the UI only managed the UI and does not contain the whole logic of database manipulation and data transformation.
what you could do is check inside the Main method if there are command line parameters and if you detect any of the special ones you have definded you really avoid to even call Application.Run(new Form1(...)); and start working in batch mode without user interface.
the same logic you want to use in batch mode or in UI mode can be wrapped in helper classes (often also called business managers or business logic... it depends), so that you do not have code duplication but simply UI or batch will call those classes nicely.
I'm new to SharpSVN (and frankly--pretty new to C# as well). I've been trying get a simple pre-commit hook working which checks for a comment. (i.e. the commit fails in the absence of a comment)
There are several posts (like this one) which are related and helpful, but I have a few fundamental questions that are keeping me from getting further:
1) How do I get code like the link above running in C#? (i.e. which C# context would I use-- console application? csharp class?)
2) In a Windows Server context, how do I call my compiled C# program?
I've tried this answer's methodology with no luck.
Thanks in advance.
If you are creating a pre-commit hook you should call it pre-commit.exe. (Subversion accepts hook with the extensions .exe, .cmd, .bat and .wsf.)
Hooks communicate via stdout, stderr and in some cases stdin, so you should compile your application as a console application.
To get the hook working you must place the .exe (and the required DLLs) in the hooks directory of the repository.
See How to access file information in a pre-commit hook using SharpSVN for some examplecode.
Compile your "hook" as a console application, and then write a batch file that calls your console application. The batch file needs to be named correctly and placed in the "hooks" folder of your Subversion repository.
For your specific case, the batch file should be called pre-commit.bat (or pre-commit.cmd).
I had to keep users from commiting to the wrong branch by mistake. So I wrote a pre-commit hook that would check the comment for a key value. If the comment doesn't start with the right key the commit is aborted.
Here is the project:
http://sourceforge.net/projects/csvnprecommit/
Feel free to use it as a base for your own hook or use it as is. If you find a bug submit it to the project.