none
Process.StartTime hangs in the middle of the function

    Pregunta

  • I have following function to make a string with process name and it's starting time.

    public static string ProcessList()
    {
    	StringBuilder result = new StringBuilder();
    	Process[] processList = Process.GetProcesses();
    			
    	foreach(Process process in processList)
    	{
    		result.Append(process.ProcessName + " " + process.StartTime.ToLocalTime().ToString());
    
            Console.WriteLine(process.StartTime.ToLocalTime().ToString());
    
    		result.Append("\n");
    	}
    	return result.ToString();
    }

    But the problem is, that it only shows first StartTime on console and thats all. It never returns any string.

    Why is that?

    sábado, 24 de abril de 2010 9:54

Respuestas

  • Hi,

    Maybe there is some security policy. for example please try following code instead...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Console.WriteLine(ProcessList());
          Console.ReadKey();
        }
        public static string ProcessList()
        {
          StringBuilder result = new StringBuilder();
          Process[] processList = Process.GetProcesses();
    
          foreach (Process process in processList)
          {
            try
            {
              result.Append(process.ProcessName + " " + process.StartTime.ToLocalTime().ToString());
              Console.WriteLine(process.StartTime.ToLocalTime().ToString());
            }
            catch (Exception ex)
            {
              result.Append(ex.Message);
            }
    
            result.Append("\n");
          }
          return result.ToString();
        }
      }
    }

    Don't be stickler and wine with William Shakespeare after the solution :^)
    "And this our life, exempt from public haunt, finds tongues in trees, books in the running brooks, sermons in stones, and good in everything." William Shakespeare
    sábado, 24 de abril de 2010 10:22