Esempio n. 1
0
        //---------------------------------------------------------------------
        private void unsealManagementPackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();

            dlg.Description = "Choose a folder where the unsealed/unpacked XML and resources will be dumped. In the case of an MPB, a subdirectory will be created.";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                if (System.IO.Path.GetExtension(m_openFileDialog.FileName).Equals(".mpb", StringComparison.InvariantCultureIgnoreCase))
                {
                    // unpack the bundle. since many files might be there, let's create a folder
                    string file      = System.IO.Path.GetFileNameWithoutExtension(m_openFileDialog.FileName);
                    string newfolder = dlg.SelectedPath + "\\" + file;
                    try
                    {
                        System.IO.Directory.CreateDirectory(newfolder);

                        ManagementPackXmlWriter mpWriter = new Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter(newfolder);

                        // if a bundle, have multiple of those mps in the bundle nd want to unpack them all
                        foreach (ManagementPack mp in m_bundle.ManagementPacks)
                        {
                            mpWriter.WriteManagementPack(mp);
                        }


                        // get a dictionary of ALL the streams from ALL the MPs and dump the streams for each MP
                        foreach (ManagementPack mp in m_bundle.ManagementPacks)
                        {
                            IDictionary <string, Stream> streams = m_bundle.GetStreams(mp);

                            foreach (KeyValuePair <string, Stream> stream in streams)
                            {
                                // safest assumption is that we already have the file name as stream.Key
                                string streamFileName = newfolder + "\\" + stream.Key;

                                foreach (ManagementPackResource resource in mp.GetResources <ManagementPackResource>())
                                {
                                    ManagementPackAssemblyResource assemblyResource = resource as ManagementPackAssemblyResource;
                                    if (assemblyResource != null &&
                                        assemblyResource.HasNullStream)
                                    {
                                        continue;
                                    }

                                    if (resource.Name.Equals(stream.Key, StringComparison.CurrentCulture))
                                    {
                                        if (!String.IsNullOrEmpty(resource.FileName))
                                        {
                                            // this filename might be preferred, if it exists
                                            streamFileName = newfolder + "\\" + resource.FileName;
                                        }
                                    }
                                }

                                // make sure we have the path (sometimes resources have nested paths like /Silverlight/ or other ones)
                                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(streamFileName));


                                // buffer stuff to copy from one stream to another
                                int    length    = 256;
                                int    bytesRead = 0;
                                Byte[] buffer    = new Byte[length];

                                // write the required bytes
                                using (FileStream fs = new FileStream(streamFileName, FileMode.Create))
                                {
                                    do
                                    {
                                        bytesRead = stream.Value.Read(buffer, 0, length);
                                        fs.Write(buffer, 0, bytesRead);
                                    }while (bytesRead == length);

                                    fs.Close();
                                }
                            }
                        }
                        MessageBox.Show(String.Format("Done extracting bundle to {0}", newfolder));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error Dumping MP bundle!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (System.IO.Path.GetExtension(m_openFileDialog.FileName).Equals(".mp", StringComparison.InvariantCultureIgnoreCase))
                {
                    // no need to reopen the file, since we have the MP already
                    ManagementPackXmlWriter mpWriter = new Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter(dlg.SelectedPath);
                    mpWriter.WriteManagementPack(m_managementPack);

                    MessageBox.Show("Unsealing done!");
                }
            }
            return;
        }
Esempio n. 2
0
        //---------------------------------------------------------------------
        private void unsealManagementPackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.Description = "Choose a folder where the unsealed/unpacked XML and resources will be dumped.";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (ManagementPack MP in m_managementPack.Values)
                {
                    ManagementPackXmlWriter mpWriter = new Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter(dlg.SelectedPath);
                    ManagementPackElementCollection<ManagementPackResource> resources = null;
                    try
                    {
                        resources = MP.GetResources<ManagementPackResource>();

                    }
                    catch
                    {

                    }

                    mpWriter.WriteManagementPack(MP);
                    if (ResourceList != null && ResourceList.Keys.Count > 0)
                    {

                        foreach (string stream in ResourceList.Keys)
                        {
                            // buffer stuff to copy from one stream to another
                            int length = 256;
                            int bytesRead = 0;
                            Byte[] buffer = new Byte[length];

                            // write the required bytes
                            using (FileStream fs = new FileStream(dlg.SelectedPath + '\\' + stream, FileMode.Create))
                            {
                                do
                                {
                                    bytesRead = ResourceList[stream].Read(buffer, 0, length);
                                    fs.Write(buffer, 0, bytesRead);
                                }
                                while (bytesRead == length);

                                fs.Close();
                            }

                        }
                    }
                }
                return;
                if (System.IO.Path.GetExtension(m_openFileDialog.FileName).Equals(".mpb", StringComparison.InvariantCultureIgnoreCase))
                {
                    // unpack the bundle. since many files might be there, let's create a folder
                    string file = System.IO.Path.GetFileNameWithoutExtension(m_openFileDialog.FileName);
                    string newfolder = dlg.SelectedPath + "\\" + file;
                    try
                    {
                        System.IO.Directory.CreateDirectory(newfolder);

                        ManagementPackXmlWriter mpWriter = new Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter(newfolder);

                        // if a bundle, have multiple of those mps in the bundle nd want to unpack them all
                        foreach (ManagementPack mp in m_bundle.ManagementPacks)
                        {
                            mpWriter.WriteManagementPack(mp);
                        }

                        foreach (string stream in ResourceList.Keys)
                        {
                            // buffer stuff to copy from one stream to another
                            int length = 256;
                            int bytesRead = 0;
                            Byte[] buffer = new Byte[length];

                            // write the required bytes
                            using (FileStream fs = new FileStream(stream, FileMode.Create))
                            {
                                do
                                {
                                    bytesRead = ResourceList[stream].Read(buffer, 0, length);
                                    fs.Write(buffer, 0, bytesRead);
                                }
                                while (bytesRead == length);

                                fs.Close();
                            }
                        }

                        // get a dictionary of ALL the streams from ALL the MPs and dump the streams for each MP
                        foreach (ManagementPack mp in m_bundle.ManagementPacks)
                        {
                            IDictionary<string, Stream> streams = m_bundle.GetStreams(mp);

                            foreach (KeyValuePair<string, Stream> stream in streams)
                            {
                                // safest assumption is that we already have the file name as stream.Key
                                string streamFileName = newfolder + "\\" + stream.Key;

                                foreach (ManagementPackResource resource in mp.GetResources<ManagementPackResource>())
                                {
                                    ManagementPackAssemblyResource assemblyResource = resource as ManagementPackAssemblyResource;
                                    if (assemblyResource != null &&
                                        assemblyResource.HasNullStream)
                                    {
                                        continue;
                                    }

                                    if (resource.Name.Equals(stream.Key, StringComparison.CurrentCulture))
                                    {
                                        if (!String.IsNullOrEmpty(resource.FileName))
                                        {
                                            // this filename might be preferred, if it exists
                                            streamFileName = newfolder + "\\" + resource.FileName;
                                        }
                                    }
                                }

                                // make sure we have the path (sometimes resources have nested paths like /Silverlight/ or other ones)
                                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(streamFileName));

                                // buffer stuff to copy from one stream to another
                                int length = 256;
                                int bytesRead = 0;
                                Byte[] buffer = new Byte[length];

                                // write the required bytes
                                using (FileStream fs = new FileStream(streamFileName, FileMode.Create))
                                {
                                    do
                                    {
                                        bytesRead = stream.Value.Read(buffer, 0, length);
                                        fs.Write(buffer, 0, bytesRead);
                                    }
                                    while (bytesRead == length);

                                    fs.Close();
                                }
                            }
                        }
                        MessageBox.Show(String.Format("Done extracting bundle to {0}", newfolder));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error Dumping MP bundle!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else if (System.IO.Path.GetExtension(m_openFileDialog.FileName).Equals(".mp", StringComparison.InvariantCultureIgnoreCase))
                {

                    // no need to reopen the file, since we have the MP already
                    ManagementPackXmlWriter mpWriter = new Microsoft.EnterpriseManagement.Configuration.IO.ManagementPackXmlWriter(dlg.SelectedPath);
                    foreach (ManagementPack MP in m_managementPack.Values)
                    {

                        mpWriter.WriteManagementPack(MP);
                    }

                    MessageBox.Show("Unsealing done!");

                }
            }
            return;
        }