I am working on Windows Form (C# application), and when I run this application from cmd.exe, the message (error log, debug log) is output to message box.
How should I do to output this message to current CMD.exe which I ran C# appliation?
Example: I do
C:>D:\Demo\CsOutputMsgToCMD\CsOutputMsgToCMD\bin\Release\CsOutputMsgToCMD.exe 1
Output: This message must be output in current CMD
The argument is 1
Update #1 : My C# application is Windows Form application (Not console) But I want when user run this application from cmd.exe
Case #2: Have argument
=> When not launch GUI app, the output must be in current cmd.exe (which ran application)
The common "trick" to making things work as per your update 1 is to have two programs. Lets call them devenv.com
and devenv.exe
. The .com
variant is a console application. The .exe
variant is a windows forms application.
If you just run devenv
from a command prompt, the console application will run (since the command prompt favours .com
over .exe
when faced with two programs with the same name but different extensions). It can then inspect its arguments and, if it decides that the GUI is called for, it launches devenv.exe
and then exits. Otherwise, it continues as normal for a console application and stays attached to the console.
If they will have a lot of common functionality, I'd recommend placing most of it in a DLL assembly that they can both share.
One approach could be changing the project type to Console Applicaton and using Environment.GetCommandLineArgs() to get the arguments. So,
Step 1: Go to project properties -> Application and change Output Type to Console Application
Step 2: Change your main method to something like this:
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length <= 1)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
switch (args[1])
{
case "1": Console.WriteLine("Doing #1 stuff"); break;
case "2": Console.WriteLine("Doing #2 stuff"); break;
case "n": Console.WriteLine("Doing #n stuff"); break;
}
}
}
The first argument would be the name of the application and the others would be the actual parameters you send in.
You should of course extract the business logic to a separate DLL to avoid code duplication. This way if you run from the console without any parameters it will launch the Form1 window otherwise will check the argument value.
You can also use Console.WriteLine statements in the forms and that output will also go to the command prompt that launched the application.
One caveat with this approach is if you double-click the application a console window opens as well as the windows form. If it doesn't bother you or running it from the command prompt is the only option you'll give then it may work for you.
Hope this helps a bit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.