locked
how to convert unserializable object into bytearray RRS feed

  • Question

  • i want to send ManagementObjectSercher to byt[] to send this over network. i was trying by serialising it but it doesn't works as this is not serializable.

    is there any other way to convert this object into byte[]. please help.


    Regards Kumar Gaurav.
    Wednesday, March 23, 2011 8:40 PM

Answers

  • Hi urprob,

    After I checked your post, could you please tell us why you want to send this information via network?

    As far as I know, you can use ConnectionOptions Class to get remote compter’s information. There are some properties in it like UserName, Password, etc.

    In addition, here’s the code snippet. You can check it.

    ConnectionOptions connection = new ConnectionOptions();
    connection.Username = userNameBox.Text;
    connection.Password = passwordBox.Text;
    connection.Authority = "ntlmdomain:DOMAIN";
    
    ManagementScope scope = new ManagementScope(
         "\\\\FullComputerName\\root\\CIMV2", connection);
    scope.Connect();
    
    ObjectQuery query= new ObjectQuery(
         "SELECT * FROM Win32_SystemUsers"); 
    
    ManagementObjectSearcher searcher = 
         new ManagementObjectSearcher(scope, query);
    
    foreach (ManagementObject queryObj in searcher.Get())
    {
         //you can get PartComponent via queryObj["PartComponent"]
    }
    Close();
    

     

     

    If you have any concern, please feel free to let us know.

    Hope this helps,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 25, 2011 2:21 AM

All replies

  • Instead of trying to serialize the instance, send parameters over the network and construct a new instance on the other side. Some classes are not serializable for good reasons. ManagementObjectSearcher is one of them as it contains machine-bound, security-sensitive data.

    Thursday, March 24, 2011 2:01 AM
  • hi,

    this is not specific to only DataTable object....

    u can serialize/deserialize any object jst type cast accordingly....

     

    private byte[] SerializeDataTable(System.Data.DataTable dtTable)
    {
      if (dtTable == default(System.Data.DataTable))
        return default(byte[]);
    
      System.IO.MemoryStream memStream = new System.IO.MemoryStream();
      System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(System.Data.DataTable));
       serializer.Serialize(memStream, dtTable);
    
       return memStream.ToArray();
    }
    
    
    private System.Data.DataTable DeserializeDataTable(byte[] tableBytes)
    {
      System.IO.MemoryStream memStream = new System.IO.MemoryStream(tableBytes);
      System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(System.Data.DataTable));        
      System.Data.DataTable dt = (System.Data.DataTable)serializer.Deserialize(memStream);
       return dt;
    }
    
    


    Nagarjuna Dilip
    Thursday, March 24, 2011 4:05 AM
  • Nagarjuna:

    You can't serialize/deserialize any objects. The System.Data.DataTable implements both ISerializable and IXmlSerializable, and that's why it works in your code.

     

    Thursday, March 24, 2011 7:29 AM
  • Hi urprob,

    After I checked your post, could you please tell us why you want to send this information via network?

    As far as I know, you can use ConnectionOptions Class to get remote compter’s information. There are some properties in it like UserName, Password, etc.

    In addition, here’s the code snippet. You can check it.

    ConnectionOptions connection = new ConnectionOptions();
    connection.Username = userNameBox.Text;
    connection.Password = passwordBox.Text;
    connection.Authority = "ntlmdomain:DOMAIN";
    
    ManagementScope scope = new ManagementScope(
         "\\\\FullComputerName\\root\\CIMV2", connection);
    scope.Connect();
    
    ObjectQuery query= new ObjectQuery(
         "SELECT * FROM Win32_SystemUsers"); 
    
    ManagementObjectSearcher searcher = 
         new ManagementObjectSearcher(scope, query);
    
    foreach (ManagementObject queryObj in searcher.Get())
    {
         //you can get PartComponent via queryObj["PartComponent"]
    }
    Close();
    

     

     

    If you have any concern, please feel free to let us know.

    Hope this helps,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 25, 2011 2:21 AM
  • Any update?

    Best Regards,


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 30, 2011 2:32 PM