0

This should be trivial: here is my attempt at the code to list running processes and serialize them to a file as list of processes:

private static void Main(string[] args)
    {
        IEnumerable<Process> processlist = Process.GetProcesses().AsEnumerable();

        XmlSerializer xmlSerializer = new XmlSerializer(processlist.GetType());

        System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:\temp\ProcessList.xml");
        xmlSerializer.Serialize(file, xmlSerializer);
        file.Close();

    }




{"Cannot serialize member 'System.ComponentModel.Component.Site' of type 'System.ComponentModel.ISite', see inner exception for more details."}

I have also tried serialising individual processes, with following code :

 private static void Main(string[] args)
    {
        IEnumerable<Process> processlist = Process.GetProcesses().AsEnumerable();

        XmlSerializer xmlSerializer = new XmlSerializer(typeof(Process));

        foreach (Process process in processlist)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter( string.Format( @"D:\temp\Process{0}.xml" , process.Id));
            xmlSerializer.Serialize(file, xmlSerializer);
            file.Close();   
        }
    }

Errors:

{"There was an error reflecting type 'System.Diagnostics.Process'."}


{"Cannot serialize member 'System.ComponentModel.Component.Site' of type 'System.ComponentModel.ISite', see inner exception for more details."}
jimjim
  • 2,414
  • 2
  • 26
  • 46

1 Answers1

2

Serializing a Process doesn't make sense. Imagine serializing your processes to xml and then deserializing them on another computer. How would you recreate a Process instance?

Maybe you're "serializing" for logging purposes? If that's the case, have you considered directly writing out just the process names (and all properties you care about) to xml.

XElement processes = new XElement("ProcessList");
foreach(var process in Process.GetProcesses().AsEnumerable())
{
    processes.Add(new XElement("Process", process.ProcessName));
}

var xmlProcessList = processes.ToString();

xmlProcessList will look something like this:

<ProcessList>
  <Process>devenv</Process>
  <Process>iexplore</Process>
  <Process>chrome</Process>
  ...
</ProcessList>
Niels Filter
  • 4,430
  • 3
  • 28
  • 42
  • +1 , Intresting point about serialization, yes you are right deserializing them does not make sense. I was after getting all the public values of the Process Properties and it's subobjects. Yes this was for logging purposes, and I wanted to have all the public properties captured in Xml. What is the alternative to logging specific properties? I want to grab all the public properties and their values and turn them into XML. I guess I look into reflection for that. – jimjim Jul 07 '15 at 06:37
  • 1
    @Arjang, you can use reflection to get all your properties. If you need only public ones, there are `BindingFlags` for that. `typeof(Process).GetProperties(BindingFlags.Public | BindingFlags.Instance)` will give you a list of public properties for `Process` class – Niels Filter Jul 07 '15 at 06:58