Hi, I use code shown below to get application name, but it shows "No "result" or "error" in XML response."
Process[] processlist = Process.GetProcesses(); Console.WriteLine(@"<?xml version=""1.0"" encoding=""UTF-8"" ?>"); Console.WriteLine(@"<prtg>"); foreach (Process process in processlist) { if (!String.IsNullOrEmpty(process.MainWindowTitle)) { Console.WriteLine(" " + @"<result>"); Console.WriteLine(" " + @"<channel>" + process.MainWindowTitle + "</channel>"); Console.WriteLine(" " + @"<unit>" + "Custom" + "</unit>"); Console.WriteLine(" " + @"<value>" + "0" + "</value>"); Console.WriteLine(" " + @"</result>"); } } Console.WriteLine(" </prtg>"); and i found a phenomenon, when i remove the for loop then hard code shown below, it Works!!! Process[] processlist = Process.GetProcesses(); Console.WriteLine(@"<?xml version=""1.0"" encoding=""UTF-8"" ?>"); Console.WriteLine(@"<prtg>"); Console.WriteLine(" " + @"<result>"); Console.WriteLine(" " + @"<channel>" + "process.MainWindowTitle" + "</channel>"); Console.WriteLine(" " + @"<unit>" + "Custom" + "</unit>"); Console.WriteLine(" " + @"<value>" + "0" + "</value>"); Console.WriteLine(" " + @"</result>"); Console.WriteLine(" " + @"<result>"); Console.WriteLine(" " + @"<channel>" + "process.MainWindowTitle" + "</channel>"); Console.WriteLine(" " + @"<unit>" + "Custom" + "</unit>"); Console.WriteLine(" " + @"<value>" + "0" + "</value>"); Console.WriteLine(" " + @"</result>"); Console.WriteLine(" </prtg>");
Could u help me to solve the problem ? Thanks.
Article Comments
Hi, this is the output, please check it. Thanks. : D
<?xml version="1.0" encoding="UTF-8" ?> <prtg> <result> <channel>Internet Information Services (IIS) ???</channel> <unit>Custom</unit> <value>0</value> </result> <result> <channel>file:///c:/users/administrator/documents/visual studio 2012/Projects/ConsoleApplication2/ConsoleApplication2/bin/Debug/ConsoleApplication2.EXE</channel> <unit>Custom</unit> <value>0</value> </result> <result> <channel>ConsoleApplication2 [??] - Microsoft Visual Studio (?????)</channel> <unit>Custom</unit> <value>0</value> </result> </prtg>
Feb, 2017 - Permalink
The output actually works in PRTG...anything else after the output in the console? Does the application need special permissions?
Feb, 2017 - Permalink
Hi, there is nothing else after the output in the console, no special permission. Here is the whole code. Thanks.
using System; using System.Diagnostics; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; class ConsoleApplication2 { public static void Main() { Process[] processlist = Process.GetProcesses(); Console.WriteLine(@"<?xml version=""1.0"" encoding=""UTF-8"" ?>"); Console.WriteLine(@"<prtg>"); foreach (Process process in processlist) { if (!String.IsNullOrEmpty(process.MainWindowTitle)) { Console.WriteLine(" " + @"<result>"); Console.WriteLine(" " + @"<channel>" + process.MainWindowTitle + "</channel>"); Console.WriteLine(" " + @"<unit>" + "Custom" + "</unit>"); Console.WriteLine(" " + @"<value>" + "0" + "</value>"); Console.WriteLine(" " + @"</result>"); } } Console.WriteLine(" </prtg>"); } }
Feb, 2017 - Permalink
What values are you actually trying to read out with that sensor? Just asking, because the Windows Process sensor might be what you're looking for. Other than that, the code looks fine... Did you try to change the sensor settings so it's execute within a different user context (Security Settings in the settings tab)?
Feb, 2017 - Permalink
I want to read the "application name" in task manager tab, just want to know what is running in server.
Feb, 2017 - Permalink
I found the application name is always null ! but i run it on local, it is ok did you solve it ?
Sep, 2017 - Permalink
As mentioned, did you try to change the sensor settings so it's executed within a different user context (Security Settings in the settings tab of the sensor)?
Kind regards, Stephan Linke
Sep, 2017 - Permalink
Yes, but it still not work. could u show me the example to list the desktop application name?
Sep, 2017 - Permalink
Are you actually using the user that has the applications opened? This may possibly not work at all due to the different sessions used by Windows in the background.
Kind regards,
Stephan Linke, Tech Support Team
Sep, 2017 - Permalink
Could you post the actual the actual output when using the for loop? Additionally, check out String.Format, it allows you to replace placeholders in strings which will actually make your code much more readable :)
Feb, 2017 - Permalink