3

I want to develop an method that when is called through the running App it's supposed to print a List but when the method is called though the CommandLine directly I want it to print the same List to a txt file.

Do you guys know how can I perform this validation or if theres is a way to detect from where the process is being called?

I found this post on stak overflow but I can't figure it out how I can solve my problem.

C#: Is it possible to have a single application behave as Console or Windows application depending on switches?

Community
  • 1
  • 1
Tiago Neto
  • 379
  • 1
  • 3
  • 15
  • can you not simply redirect the output of the console app to a file? MyApp > aFile.txt – vc 74 Aug 07 '15 at 10:20
  • Just to clarify: It's not about which command line parameters are passed, but really about whether the app was run by typing "myApp.exe" into the command line vs e.g. clicking an icon? – Sebastian Negraszus Aug 07 '15 at 10:24
  • Yes that's it! I have two scenarios: 1) Running an app that consists in a menu in the console and each option performs an operation 2) Call that option directly from the Command Line Both ways should have a different behaviour – Tiago Neto Aug 07 '15 at 10:47

2 Answers2

2

You could try grabbing the console window from a kernel 32 function call.

private const string Kernel32_DllName = "kernel32.dll";

[DllImport(Kernel32_DllName)]
private static extern IntPtr GetConsoleWindow();

public static bool HasConsole {
    get { return GetConsoleWindow() != IntPtr.Zero; }
}

HasConsole should return true if there is a console loaded, false if launched from a window.

If the program is a console application, there will always be a console window open, so detecting the launch method is more tricky.

There is an SO question covering this already - How can you determine how a console application was launched?

As a quick reference, the relevant section is listed below, but I would recommend having a read through that thread as it explains the process more clearly.

static bool StartedFromGui = 
     !Console.IsOutputRedirected
  && !Console.IsInputRedirected
  && !Console.IsErrorRedirected
  && Environment.UserInteractive
  && Environment.CurrentDirectory == System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
  && Console.CursorTop == 0 && Console.CursorLeft == 0
  && Console.Title == Environment.GetCommandLineArgs()[0]
  && Environment.GetCommandLineArgs()[0] == System.Reflection.Assembly.GetEntryAssembly().Location;
Community
  • 1
  • 1
Alex
  • 1,643
  • 1
  • 14
  • 32
  • I believe it wouldn't work because the native app runs in the Console itself (Its a menu with operations to perform). So as the application is already running in the Console the HasConsole will allways return 'True' in both scenarios right? (Calling the operation directly from the Command Line or though the option menu) – Tiago Neto Aug 07 '15 at 11:02
  • Ok, so this is a console application which can be launched form an icon, or from a console window? If so, regardless of which you chose, it will always create a console window, so yes, you are right, it will always return true. I will update my answer. – Alex Aug 07 '15 at 11:05
  • Where abouts have you added the definition? That code snippet should be placed in your Program.cs (or whatever program contains your Main), and should be the first statement inside the class body. – Alex Aug 07 '15 at 12:58
0

Hey you could try this to detect if you are running from the console or not bool runningFromConsole = Console.OpenStandardInput(1) != Stream.Null;

Chino
  • 821
  • 6
  • 13
  • A silly question, if the method is being called trough the Command Line the runningFromConsole is going to return 'True' right? I'm affraid it wouldn't work because the native app runs in the console (It's a menu that the user can select the operations to perform) but it's also possible to call that operation directly from the Command Line. As the app run in the console the variable runningFromConsole will return 'true' in both scenarios right? – Tiago Neto Aug 07 '15 at 10:56