locked
How to set Content field of FileAttachment RRS feed

  • Question

  • I am working on creating my own File Attachment section on a custom form.  I am having difficulty with determining how to set the 'Content' field of System.FileAttachment.  I open a file dialog and read the selected file into a byte array.  From there, I'm not sure how to get it to the proper format, or even what format SCSM is expecting when setting this field.  Relevant code:

     
    ManagementPack mpFileAttachment = emg.ManagementPacks.GetManagementPack(new Guid("23e3ae8e-1981-8560-2e55-8730cbc04965"));
    ManagementPackClass mpcFileAttachment = mpFileAttachment.GetClass("System.FileAttachment");
    CreatableEnterpriseManagementObject cemoAttachment = new CreatableEnterpriseManagementObject(emg, mpcFileAttachment);
    
    OpenFileDialog ofd = new OpenFileDialog();
    if(ofd.ShowDialog() == DialogResult.OK)
    {
      string fPath = ofd.FileName;
      byte[] anotherOneBytesTheDust;
      int fLen;
      try
      {
        using (FileStream fs = File.OpenRead(fPath))
        {
          fLen = (int)fs.Length;
          anotherOneBytesTheDust = new byte[fLen];
          fs.Read(anotherOneBytesTheDust, 0, fLen);
        }
      }
      catch (Exception excep)
      {
        MessageBox.Show("Read Error: " + excep.ToString());
        return;
      }
                            
      try
      {                    
        cemoAttachment[mpcFileAttachment, "Content"].Value = anotherOneBytesTheDust;
      }
      catch (Exception excep)
      {
        MessageBox.Show("Content Error: " + excep.ToString());
      }
    }

    Tuesday, August 27, 2019 2:01 PM

Answers

  • To answer my own question it takes in a Stream variable.  The corrected code would be:
    ManagementPack mpFileAttachment = emg.ManagementPacks.GetManagementPack(new Guid("23e3ae8e-1981-8560-2e55-8730cbc04965"));
    ManagementPackClass mpcFileAttachment = mpFileAttachment.GetClass("System.FileAttachment");
    CreatableEnterpriseManagementObject cemoAttachment = new CreatableEnterpriseManagementObject(emg, mpcFileAttachment);
    
    OpenFileDialog ofd = new OpenFileDialog();
    if(ofd.ShowDialog() == DialogResult.OK)
    {
      string fPath = ofd.FileName;
      int fLen;
      try
      {
        using (FileStream fs = File.OpenRead(fPath))
        {
          fLen = (int)fs.Length;
          try
          {                    
            cemoAttachment[mpcFileAttachment, "Content"].Value = fs;
          }
          catch (Exception excep)
          {
            MessageBox.Show("Content Error: " + excep.ToString());
          }
        }
      }
      catch (Exception excep)
      {
        MessageBox.Show("Read Error: " + excep.ToString());
        return;
      }
                            
      
    }

    • Marked as answer by Ryan Acton Wednesday, August 28, 2019 7:54 PM
    Wednesday, August 28, 2019 7:54 PM