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
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
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
}
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
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