1

I have server that gets process list, then it converts it to comma-separated string and then sends it.

var processlist = Process.GetProcesses();
string sendata = string.Join(",", processlist.Select(c => c.ToString()).ToArray<string>());
writer.WriteLine(sendata);
writer.Flush();

Now on the other side I receive string and I change it to string[].

string[] split = response.Split(',');

Now how can I change it from string[] back to Process[]?

8
  • Though you can instantiate new Process objects and give them the name what do you get from it? A Process object that you get from GetProcesses has so much more information than just a name.. Commented Apr 8, 2018 at 9:22
  • Ok, so how can I send it over tcp? It's my first project with sending data through TCP Commented Apr 8, 2018 at 9:24
  • @AdrianRudyDacka : I felt you only interested in sending process names to other side of wire ? what you want to do with these names "on the other side" ? Commented Apr 8, 2018 at 9:27
  • I want to show them in gridview, but i dont want only name. I want to transfer all info and show it in gridview Commented Apr 8, 2018 at 9:29
  • 2
    What you're suggesting doesn't make any sense. A Process object actually represents a process running on the machine. The same process isn't running on the other machine so how could knowing the name of the process possibly help? That's like me sending you the name of a person in my house and you being able to determine that persons being and weight simply from that. You need to gather all the information you need from the Process objects on the source machine and send it ALL. You don't have to send Process objects, just objects that contain the information you need. Commented Apr 8, 2018 at 10:12

1 Answer 1

1

How about something like this? Note I didn't make any attempt to compile this.

You have to copy over all the properties of the Process and transmit them over the wire. On the other side, you can get out the properties and their values for display, although you will not have a real Process object.

public struct PropertyValue {
    public PropertyValue( string propertyName, string value) 
    { 
        this.PropertyName = propertyName;
        this.Value = this.value;
    }
    private PropertyValue( params string[] pv) => PropertyValue(pv[0], pv[1]);
    public string PropertyName {get;}
    public string Value {get;}
    override public string ToString() => this.PropertyName + ":" + this.Value;
    public static PropertyValue FromString(string pv) => return new PropertyValue( pv.Split(":"));
}

and then

public string ToString( Process process) {
    var msg = new StringBuffer();
    foreach (PropertyInfo property in process.GetProperties()) {
         var propertyValue = new PropertyValue( property.Name, property.GetValue( process));
         msg.Append( ((msg.Length > 0)? ",": "") + propertyValue.ToString());
    }
    return msg.ToString();
}

public PropertyValue[] FromString( string msg) 
{
    string[] pvStrings = msg.Split(",");
    PropertyValue[] propertyValues = new PropertyValue[pvStrings.Length];
    int i = 0;
    foreach (string pvString in pvStrings) {
        propertyValues[i] = FromString( pvString);
    }
    return propertyValues;
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.