2

Is there a way to detect if a c# xamarin console library is being run or being run via a terminal? this is so i can add

console.read();

if the user is directly running the program so they can see the output of the program

davidallyoung
  • 1,302
  • 11
  • 15
AlexanderRD
  • 2,069
  • 2
  • 11
  • 19

3 Answers3

0

Something like this should put you in the right direction: (https://msdn.microsoft.com/en-us/library/z3w4xdc9(v=vs.110).aspx)

var processes = Process.GetProcessesByName("Name");
if(processes.Count > 0)
{
    //do something here
}
brettkc
  • 252
  • 1
  • 9
0

Using the command

Environment.CommandLine

allows the command that opened the program. if the program is run in command line the Environment.CommandLine will show

ProgramName -parameter

However if it is called via explorer or a debug program it will be called as

 "PathToFile/ProgramName.exe"

This allows the process to see if it is called via explorer or CMD via some if statments

AlexanderRD
  • 2,069
  • 2
  • 11
  • 19
  • 1
    Not necessarily. You cannot rely on this to make the distinction. – Cody Gray - on strike Jun 12 '16 at 16:54
  • What can I rely on to make the distinction? – AlexanderRD Jun 12 '16 at 18:23
  • Well, that is why you asked the question. :-) I can think of a couple of better solutions, but was too lazy to write up an answer to a question tagged C#, complete with p/invoke code. But if you're looking for a pointer in the right direction, you might try calling `GetStdHandle` and checking to see if it has a stdout handle. A standard Windows application will not have one, unless someone has specifically given it one, in which case it is perfectly logical for you to write to it using `Console.Read`. Console applications have these handles available by default, of course. – Cody Gray - on strike Jun 13 '16 at 11:19
  • Or, there might a .NET of doing the same thing. Maybe something like `Console.OpenStandardInput`, which you can check to see if it returns a null stream. Not sure, I've never tried it. I'd look at the reference source code to confirm what it actually does. Another straightforward option in Win32 would be calling the `GetConsoleWindow` function. If there's a console window attached to the process, it will return true; otherwise, it returns NULL. Problem with that is, you can be running as a console app with an invisible console window. In that case, you can still write output to stdout, though – Cody Gray - on strike Jun 13 '16 at 11:31
  • so this may not get you the information you are seeking. You have to be more precise about the question. You probably don't actually want or need to know if you're being run via the command prompt. What you really want to know is if it's legal to read/write to/from the console. Which is a very similar question, but one that can be easily and correctly answered. Anyway, the purpose of my original comment was merely to indicate that *your* answer was wrong and may produce misleading results. And look, now I've written 3 comments. Should have just written an answer... – Cody Gray - on strike Jun 13 '16 at 11:32
0

If you're running from the command line then the MainWindowTitle will be empty. So let's check first if we are runnning UserInteractive and then if the title is blank.

private bool GUIMode()
{
    return Environment.UserInteractive && !string.IsNullOrEmpty(Process.GetCurrentProcess().MainWindowTitle);
}

OR VB


Private Function GUIMode() As Boolean
    Return Environment.UserInteractive AndAlso Not String.IsNullOrEmpty(Process.GetCurrentProcess().MainWindowTitle)
End Function

HackSlash
  • 4,944
  • 2
  • 18
  • 44