diff --git a/sources/Structured Storage Explorer/MainForm.Designer.cs b/sources/Structured Storage Explorer/MainForm.Designer.cs index 07e1bd73..a20ed86b 100644 --- a/sources/Structured Storage Explorer/MainForm.Designer.cs +++ b/sources/Structured Storage Explorer/MainForm.Designer.cs @@ -96,9 +96,8 @@ private void InitializeComponent() this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill; this.treeView1.HideSelection = false; this.treeView1.Location = new System.Drawing.Point(0, 0); - this.treeView1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.treeView1.Name = "treeView1"; - this.treeView1.Size = new System.Drawing.Size(423, 319); + this.treeView1.Size = new System.Drawing.Size(281, 201); this.treeView1.TabIndex = 4; this.treeView1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseUp); // @@ -112,41 +111,41 @@ private void InitializeComponent() this.addStreamToolStripMenuItem, this.removeToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; - this.contextMenuStrip1.Size = new System.Drawing.Size(196, 164); + this.contextMenuStrip1.Size = new System.Drawing.Size(148, 114); this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening); // // importDataStripMenuItem1 // this.importDataStripMenuItem1.Name = "importDataStripMenuItem1"; - this.importDataStripMenuItem1.Size = new System.Drawing.Size(195, 32); + this.importDataStripMenuItem1.Size = new System.Drawing.Size(147, 22); this.importDataStripMenuItem1.Text = "Import data..."; this.importDataStripMenuItem1.Click += new System.EventHandler(this.importDataStripMenuItem1_Click); // // exportDataToolStripMenuItem // this.exportDataToolStripMenuItem.Name = "exportDataToolStripMenuItem"; - this.exportDataToolStripMenuItem.Size = new System.Drawing.Size(195, 32); + this.exportDataToolStripMenuItem.Size = new System.Drawing.Size(147, 22); this.exportDataToolStripMenuItem.Text = "Export data..."; this.exportDataToolStripMenuItem.Click += new System.EventHandler(this.exportDataToolStripMenuItem_Click); // // addStorageStripMenuItem1 // this.addStorageStripMenuItem1.Name = "addStorageStripMenuItem1"; - this.addStorageStripMenuItem1.Size = new System.Drawing.Size(195, 32); + this.addStorageStripMenuItem1.Size = new System.Drawing.Size(147, 22); this.addStorageStripMenuItem1.Text = "Add storage..."; this.addStorageStripMenuItem1.Click += new System.EventHandler(this.addStorageStripMenuItem1_Click); // // addStreamToolStripMenuItem // this.addStreamToolStripMenuItem.Name = "addStreamToolStripMenuItem"; - this.addStreamToolStripMenuItem.Size = new System.Drawing.Size(195, 32); + this.addStreamToolStripMenuItem.Size = new System.Drawing.Size(147, 22); this.addStreamToolStripMenuItem.Text = "Add stream..."; this.addStreamToolStripMenuItem.Click += new System.EventHandler(this.addStreamToolStripMenuItem_Click); // // removeToolStripMenuItem // this.removeToolStripMenuItem.Name = "removeToolStripMenuItem"; - this.removeToolStripMenuItem.Size = new System.Drawing.Size(195, 32); + this.removeToolStripMenuItem.Size = new System.Drawing.Size(147, 22); this.removeToolStripMenuItem.Text = "Remove"; this.removeToolStripMenuItem.Click += new System.EventHandler(this.removeToolStripMenuItem_Click); // @@ -157,15 +156,14 @@ private void InitializeComponent() // // menuStrip1 // - this.menuStrip1.GripMargin = new System.Windows.Forms.Padding(2, 2, 0, 2); this.menuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, this.editToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; - this.menuStrip1.Padding = new System.Windows.Forms.Padding(9, 2, 0, 2); - this.menuStrip1.Size = new System.Drawing.Size(1280, 33); + this.menuStrip1.Padding = new System.Windows.Forms.Padding(6, 1, 0, 1); + this.menuStrip1.Size = new System.Drawing.Size(853, 24); this.menuStrip1.TabIndex = 5; this.menuStrip1.Text = "menuStrip1"; // @@ -179,14 +177,14 @@ private void InitializeComponent() this.updateCurrentFileToolStripMenuItem, this.saveAsToolStripMenuItem}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; - this.fileToolStripMenuItem.Size = new System.Drawing.Size(54, 29); + this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 22); this.fileToolStripMenuItem.Text = "File"; // // openFileMenuItem // this.openFileMenuItem.Image = global::StructuredStorageExplorer.Properties.Resources.folder; this.openFileMenuItem.Name = "openFileMenuItem"; - this.openFileMenuItem.Size = new System.Drawing.Size(276, 34); + this.openFileMenuItem.Size = new System.Drawing.Size(187, 26); this.openFileMenuItem.Text = "Open..."; this.openFileMenuItem.Click += new System.EventHandler(this.openFileMenuItem_Click); // @@ -194,34 +192,34 @@ private void InitializeComponent() // this.newStripMenuItem1.Image = global::StructuredStorageExplorer.Properties.Resources.page_white; this.newStripMenuItem1.Name = "newStripMenuItem1"; - this.newStripMenuItem1.Size = new System.Drawing.Size(276, 34); + this.newStripMenuItem1.Size = new System.Drawing.Size(187, 26); this.newStripMenuItem1.Text = "New Compound File"; this.newStripMenuItem1.Click += new System.EventHandler(this.newStripMenuItem1_Click); // // closeStripMenuItem1 // this.closeStripMenuItem1.Name = "closeStripMenuItem1"; - this.closeStripMenuItem1.Size = new System.Drawing.Size(276, 34); + this.closeStripMenuItem1.Size = new System.Drawing.Size(187, 26); this.closeStripMenuItem1.Text = "Close file"; this.closeStripMenuItem1.Click += new System.EventHandler(this.closeStripMenuItem1_Click); // // toolStripSeparator2 // this.toolStripSeparator2.Name = "toolStripSeparator2"; - this.toolStripSeparator2.Size = new System.Drawing.Size(273, 6); + this.toolStripSeparator2.Size = new System.Drawing.Size(184, 6); // // updateCurrentFileToolStripMenuItem // this.updateCurrentFileToolStripMenuItem.Image = global::StructuredStorageExplorer.Properties.Resources.disk; this.updateCurrentFileToolStripMenuItem.Name = "updateCurrentFileToolStripMenuItem"; - this.updateCurrentFileToolStripMenuItem.Size = new System.Drawing.Size(276, 34); + this.updateCurrentFileToolStripMenuItem.Size = new System.Drawing.Size(187, 26); this.updateCurrentFileToolStripMenuItem.Text = "Save"; this.updateCurrentFileToolStripMenuItem.Click += new System.EventHandler(this.updateCurrentFileToolStripMenuItem_Click); // // saveAsToolStripMenuItem // this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem"; - this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(276, 34); + this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(187, 26); this.saveAsToolStripMenuItem.Text = "Save As..."; this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click); // @@ -230,13 +228,13 @@ private void InitializeComponent() this.editToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.preferencesToolStripMenuItem}); this.editToolStripMenuItem.Name = "editToolStripMenuItem"; - this.editToolStripMenuItem.Size = new System.Drawing.Size(58, 29); + this.editToolStripMenuItem.Size = new System.Drawing.Size(39, 22); this.editToolStripMenuItem.Text = "Edit"; // // preferencesToolStripMenuItem // this.preferencesToolStripMenuItem.Name = "preferencesToolStripMenuItem"; - this.preferencesToolStripMenuItem.Size = new System.Drawing.Size(204, 34); + this.preferencesToolStripMenuItem.Size = new System.Drawing.Size(135, 22); this.preferencesToolStripMenuItem.Text = "Preferences"; this.preferencesToolStripMenuItem.Click += new System.EventHandler(this.preferencesToolStripMenuItem_Click); // @@ -245,24 +243,22 @@ private void InitializeComponent() this.statusStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileNameLabel}); - this.statusStrip1.Location = new System.Drawing.Point(0, 682); + this.statusStrip1.Location = new System.Drawing.Point(0, 436); this.statusStrip1.Name = "statusStrip1"; - this.statusStrip1.Padding = new System.Windows.Forms.Padding(1, 0, 21, 0); - this.statusStrip1.Size = new System.Drawing.Size(1280, 22); + this.statusStrip1.Size = new System.Drawing.Size(853, 22); this.statusStrip1.TabIndex = 6; this.statusStrip1.Text = "statusStrip1"; // // fileNameLabel // this.fileNameLabel.Name = "fileNameLabel"; - this.fileNameLabel.Size = new System.Drawing.Size(0, 15); + this.fileNameLabel.Size = new System.Drawing.Size(0, 17); // // splitContainer1 // this.splitContainer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer1.Location = new System.Drawing.Point(0, 0); - this.splitContainer1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.splitContainer1.Name = "splitContainer1"; this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal; // @@ -273,26 +269,23 @@ private void InitializeComponent() // splitContainer1.Panel2 // this.splitContainer1.Panel2.Controls.Add(this.propertyGrid1); - this.splitContainer1.Size = new System.Drawing.Size(425, 649); - this.splitContainer1.SplitterDistance = 321; - this.splitContainer1.SplitterWidth = 6; + this.splitContainer1.Size = new System.Drawing.Size(283, 412); + this.splitContainer1.SplitterDistance = 203; this.splitContainer1.TabIndex = 5; // // propertyGrid1 // this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill; this.propertyGrid1.Location = new System.Drawing.Point(0, 0); - this.propertyGrid1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.propertyGrid1.Name = "propertyGrid1"; - this.propertyGrid1.Size = new System.Drawing.Size(423, 320); + this.propertyGrid1.Size = new System.Drawing.Size(281, 203); this.propertyGrid1.TabIndex = 0; this.propertyGrid1.ToolbarVisible = false; // // splitContainer2 // this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill; - this.splitContainer2.Location = new System.Drawing.Point(0, 33); - this.splitContainer2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.splitContainer2.Location = new System.Drawing.Point(0, 24); this.splitContainer2.Name = "splitContainer2"; // // splitContainer2.Panel1 @@ -302,9 +295,8 @@ private void InitializeComponent() // splitContainer2.Panel2 // this.splitContainer2.Panel2.Controls.Add(this.tabControl1); - this.splitContainer2.Size = new System.Drawing.Size(1280, 649); - this.splitContainer2.SplitterDistance = 425; - this.splitContainer2.SplitterWidth = 6; + this.splitContainer2.Size = new System.Drawing.Size(853, 412); + this.splitContainer2.SplitterDistance = 283; this.splitContainer2.TabIndex = 7; // // tabControl1 @@ -313,20 +305,18 @@ private void InitializeComponent() this.tabControl1.Controls.Add(this.tabPage2); this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill; this.tabControl1.Location = new System.Drawing.Point(0, 0); - this.tabControl1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; - this.tabControl1.Size = new System.Drawing.Size(849, 649); + this.tabControl1.Size = new System.Drawing.Size(566, 412); this.tabControl1.TabIndex = 1; // // tabPage1 // this.tabPage1.Controls.Add(this.hexEditor); - this.tabPage1.Location = new System.Drawing.Point(4, 29); - this.tabPage1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.tabPage1.Location = new System.Drawing.Point(4, 22); this.tabPage1.Name = "tabPage1"; - this.tabPage1.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5); - this.tabPage1.Size = new System.Drawing.Size(841, 616); + this.tabPage1.Padding = new System.Windows.Forms.Padding(3, 3, 3, 3); + this.tabPage1.Size = new System.Drawing.Size(558, 386); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "Raw Data"; this.tabPage1.UseVisualStyleBackColor = true; @@ -336,13 +326,12 @@ private void InitializeComponent() this.hexEditor.BackColor = System.Drawing.Color.WhiteSmoke; this.hexEditor.Dock = System.Windows.Forms.DockStyle.Fill; this.hexEditor.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.hexEditor.LineInfoForeColor = System.Drawing.Color.Empty; + this.hexEditor.InfoForeColor = System.Drawing.Color.Empty; this.hexEditor.LineInfoVisible = true; - this.hexEditor.Location = new System.Drawing.Point(4, 5); - this.hexEditor.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.hexEditor.Location = new System.Drawing.Point(3, 3); this.hexEditor.Name = "hexEditor"; this.hexEditor.ShadowSelectionColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(60)))), ((int)(((byte)(188)))), ((int)(((byte)(255))))); - this.hexEditor.Size = new System.Drawing.Size(833, 606); + this.hexEditor.Size = new System.Drawing.Size(552, 380); this.hexEditor.StringViewVisible = true; this.hexEditor.TabIndex = 0; this.hexEditor.UseFixedBytesPerLine = true; @@ -351,11 +340,10 @@ private void InitializeComponent() // tabPage2 // this.tabPage2.Controls.Add(this.splitContainer3); - this.tabPage2.Location = new System.Drawing.Point(4, 29); - this.tabPage2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); + this.tabPage2.Location = new System.Drawing.Point(4, 22); this.tabPage2.Name = "tabPage2"; - this.tabPage2.Padding = new System.Windows.Forms.Padding(4, 5, 4, 5); - this.tabPage2.Size = new System.Drawing.Size(841, 616); + this.tabPage2.Padding = new System.Windows.Forms.Padding(3, 3, 3, 3); + this.tabPage2.Size = new System.Drawing.Size(558, 396); this.tabPage2.TabIndex = 1; this.tabPage2.Text = "OLE Properties"; this.tabPage2.UseVisualStyleBackColor = true; @@ -363,8 +351,8 @@ private void InitializeComponent() // splitContainer3 // this.splitContainer3.Dock = System.Windows.Forms.DockStyle.Fill; - this.splitContainer3.Location = new System.Drawing.Point(4, 5); - this.splitContainer3.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); + this.splitContainer3.Location = new System.Drawing.Point(3, 3); + this.splitContainer3.Margin = new System.Windows.Forms.Padding(2, 3, 2, 3); this.splitContainer3.Name = "splitContainer3"; this.splitContainer3.Orientation = System.Windows.Forms.Orientation.Horizontal; // @@ -375,9 +363,9 @@ private void InitializeComponent() // splitContainer3.Panel2 // this.splitContainer3.Panel2.Controls.Add(this.dgvUserDefinedProperties); - this.splitContainer3.Size = new System.Drawing.Size(833, 606); - this.splitContainer3.SplitterDistance = 303; - this.splitContainer3.SplitterWidth = 5; + this.splitContainer3.Size = new System.Drawing.Size(552, 390); + this.splitContainer3.SplitterDistance = 195; + this.splitContainer3.SplitterWidth = 3; this.splitContainer3.TabIndex = 2; // // dgvOLEProps @@ -385,10 +373,9 @@ private void InitializeComponent() this.dgvOLEProps.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dgvOLEProps.Dock = System.Windows.Forms.DockStyle.Fill; this.dgvOLEProps.Location = new System.Drawing.Point(0, 0); - this.dgvOLEProps.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.dgvOLEProps.Name = "dgvOLEProps"; this.dgvOLEProps.RowHeadersWidth = 62; - this.dgvOLEProps.Size = new System.Drawing.Size(833, 303); + this.dgvOLEProps.Size = new System.Drawing.Size(552, 195); this.dgvOLEProps.TabIndex = 0; // // dgvUserDefinedProperties @@ -396,22 +383,20 @@ private void InitializeComponent() this.dgvUserDefinedProperties.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dgvUserDefinedProperties.Dock = System.Windows.Forms.DockStyle.Fill; this.dgvUserDefinedProperties.Location = new System.Drawing.Point(0, 0); - this.dgvUserDefinedProperties.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.dgvUserDefinedProperties.Name = "dgvUserDefinedProperties"; this.dgvUserDefinedProperties.RowHeadersWidth = 62; - this.dgvUserDefinedProperties.Size = new System.Drawing.Size(833, 298); + this.dgvUserDefinedProperties.Size = new System.Drawing.Size(552, 192); this.dgvUserDefinedProperties.TabIndex = 1; // // MainForm // - this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1280, 704); + this.ClientSize = new System.Drawing.Size(853, 458); this.Controls.Add(this.splitContainer2); this.Controls.Add(this.statusStrip1); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; - this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.Name = "MainForm"; this.Text = "Structured Storage eXplorer"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); diff --git a/sources/Structured Storage Explorer/MainForm.cs b/sources/Structured Storage Explorer/MainForm.cs index 26c128fd..83776c17 100644 --- a/sources/Structured Storage Explorer/MainForm.cs +++ b/sources/Structured Storage Explorer/MainForm.cs @@ -66,11 +66,11 @@ private void OpenFile() private void CloseCurrentFile() { - if (cf != null) - cf.Close(); + cf?.Close(); + cf = null; - if (fs != null) - fs.Close(); + fs?.Close(); + fs = null; treeView1.Nodes.Clear(); fileNameLabel.Text = string.Empty; @@ -108,7 +108,7 @@ private void RefreshTree() root.ImageIndex = 0; root.Tag = cf.RootStorage; - //Recursive function to get all storage and streams + // Recursive function to get all storage and streams AddNodes(root, cf.RootStorage); } @@ -123,33 +123,26 @@ private void LoadFile(string fileName, bool enableCommit) try { - if (cf != null) - { - cf.Close(); - cf = null; - } + cf?.Close(); + cf = null; CFSConfiguration cfg = CFSConfiguration.SectorRecycle | CFSConfiguration.EraseFreeSectors; if (!Settings.Default.EnableValidation) cfg |= CFSConfiguration.NoValidationException; - //Load file - if (enableCommit) - { - cf = new CompoundFile(fs, CFSUpdateMode.Update, cfg); - } - else - { - cf = new CompoundFile(fs); - } + // Load file + cf = enableCommit ? new CompoundFile(fs, CFSUpdateMode.Update, cfg) : new CompoundFile(fs); RefreshTree(); } catch (Exception ex) { cf?.Close(); + cf = null; + fs?.Close(); + fs = null; treeView1.Nodes.Clear(); fileNameLabel.Text = string.Empty; @@ -161,62 +154,57 @@ private void LoadFile(string fileName, bool enableCommit) /// Recursive addition of tree nodes foreach child of current item in the storage /// /// Current TreeNode - /// Current storage associated with node - private static void AddNodes(TreeNode node, CFStorage cfs) + /// Current storage associated with node + private static void AddNodes(TreeNode node, CFStorage storage) { - Action va = delegate (CFItem target) + void va(CFItem item) { - TreeNode temp = node.Nodes.Add( - target.Name, - target.Name + (target.IsStream ? " (" + target.Size + " bytes )" : "")); + TreeNode childNode = node.Nodes.Add( + item.Name, + item.Name + (item.IsStream ? " (" + item.Size + " bytes )" : "")); - temp.Tag = target; + childNode.Tag = item; - if (target.IsStream) + if (item is CFStorage subStorage) { - //Stream - temp.ImageIndex = 1; - temp.SelectedImageIndex = 1; + // Storage + childNode.ImageIndex = 0; + childNode.SelectedImageIndex = 0; + + // Recursion into the storage + AddNodes(childNode, subStorage); } else { - //Storage - temp.ImageIndex = 0; - temp.SelectedImageIndex = 0; - - //Recursion into the storage - AddNodes(temp, (CFStorage)target); + // Stream + childNode.ImageIndex = 1; + childNode.SelectedImageIndex = 1; } - }; + } - //Visit NON-recursively (first level only) - cfs.VisitEntries(va, false); + // Visit NON-recursively (first level only) + storage.VisitEntries(va, false); } private void exportDataToolStripMenuItem_Click(object sender, EventArgs e) { - //No export if storage - if (treeView1.SelectedNode == null || !((CFItem)treeView1.SelectedNode.Tag).IsStream) + // No export if storage + if (treeView1.SelectedNode == null + || treeView1.SelectedNode.Tag is not CFStream stream) { MessageBox.Show("Only stream data can be exported", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); - return; } - CFStream target = (CFStream)treeView1.SelectedNode.Tag; - // A lot of stream and storage have only non-printable characters. // We need to sanitize filename. string sanitizedFileName = string.Empty; - foreach (char c in target.Name) + foreach (char c in stream.Name) { - if ( - char.GetUnicodeCategory(c) == UnicodeCategory.LetterNumber - || char.GetUnicodeCategory(c) == UnicodeCategory.LowercaseLetter - || char.GetUnicodeCategory(c) == UnicodeCategory.UppercaseLetter) - + UnicodeCategory category = char.GetUnicodeCategory(c); + if (category is UnicodeCategory.LetterNumber or UnicodeCategory.LowercaseLetter or UnicodeCategory.UppercaseLetter) sanitizedFileName += c; } @@ -225,29 +213,19 @@ private void exportDataToolStripMenuItem_Click(object sender, EventArgs e) sanitizedFileName = "tempFileName"; } - saveFileDialog1.FileName = sanitizedFileName + ".bin"; + saveFileDialog1.FileName = $"{sanitizedFileName}.bin"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { - FileStream fs = null; - try { - fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.ReadWrite); - fs.Write(target.GetData(), 0, (int)target.Size); + using FileStream fs = new(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.ReadWrite); + fs.Write(stream.GetData(), 0, (int)stream.Size); } catch (Exception ex) { treeView1.Nodes.Clear(); - MessageBox.Show("Internal error: " + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - finally - { - if (fs != null) - { - fs.Flush(); - fs.Close(); - } + MessageBox.Show($"Internal error: {ex.Message}", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -255,7 +233,8 @@ private void exportDataToolStripMenuItem_Click(object sender, EventArgs e) private void removeToolStripMenuItem_Click(object sender, EventArgs e) { TreeNode n = treeView1.SelectedNode; - ((CFStorage)n.Parent.Tag).Delete(n.Name); + if (n?.Parent?.Tag is CFStorage storage) + storage.Delete(n.Name); RefreshTree(); } @@ -278,27 +257,25 @@ private void updateCurrentFileToolStripMenuItem_Click(object sender, EventArgs e cf.Commit(); } else + { MessageBox.Show("Cannot update a compound document that is not based on a stream or on a file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void addStreamToolStripMenuItem_Click(object sender, EventArgs e) { string streamName = string.Empty; - if (Utils.InputBox("Add stream", "Insert stream name", ref streamName) == DialogResult.OK) + if (Utils.InputBox("Add stream", "Insert stream name", ref streamName) == DialogResult.OK + && treeView1.SelectedNode.Tag is CFStorage storage) { - CFItem cfs = treeView1.SelectedNode.Tag as CFItem; - - if (cfs != null && (cfs.IsStorage || cfs.IsRoot)) + try { - try - { - ((CFStorage)cfs).AddStream(streamName); - } - catch (CFDuplicatedItemException) - { - MessageBox.Show("Cannot insert a duplicated item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + storage.AddStream(streamName); + } + catch (CFDuplicatedItemException) + { + MessageBox.Show("Cannot insert a duplicated item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } RefreshTree(); @@ -307,22 +284,18 @@ private void addStreamToolStripMenuItem_Click(object sender, EventArgs e) private void addStorageStripMenuItem1_Click(object sender, EventArgs e) { - string storage = string.Empty; + string storageName = string.Empty; - if (Utils.InputBox("Add storage", "Insert storage name", ref storage) == DialogResult.OK) + if (Utils.InputBox("Add storage", "Insert storage name", ref storageName) == DialogResult.OK + && treeView1.SelectedNode.Tag is CFStorage storage) { - CFItem cfs = treeView1.SelectedNode.Tag as CFItem; - - if (cfs != null && (cfs.IsStorage || cfs.IsRoot)) + try { - try - { - ((CFStorage)cfs).AddStorage(storage); - } - catch (CFDuplicatedItemException) - { - MessageBox.Show("Cannot insert a duplicated item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + storage.AddStorage(storageName); + } + catch (CFDuplicatedItemException) + { + MessageBox.Show("Cannot insert a duplicated item", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } RefreshTree(); @@ -331,28 +304,24 @@ private void addStorageStripMenuItem1_Click(object sender, EventArgs e) private void importDataStripMenuItem1_Click(object sender, EventArgs e) { - if (openDataFileDialog.ShowDialog() == DialogResult.OK) + if (openDataFileDialog.ShowDialog() == DialogResult.OK + && treeView1.SelectedNode.Tag is CFStream stream) { - CFStream s = treeView1.SelectedNode.Tag as CFStream; + using FileStream f = new(openDataFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); + byte[] data = new byte[f.Length]; + f.Read(data, 0, (int)f.Length); + f.Flush(); + f.Close(); + stream.SetData(data); - if (s != null) - { - FileStream f = new FileStream(openDataFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read); - byte[] data = new byte[f.Length]; - f.Read(data, 0, (int)f.Length); - f.Flush(); - f.Close(); - s.SetData(data); - - RefreshTree(); - } + RefreshTree(); } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { - if (cf != null) - cf.Close(); + cf?.Close(); + cf = null; } private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) @@ -372,149 +341,140 @@ private void openFileMenuItem_Click(object sender, EventArgs e) { OpenFile(); } - catch + catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException || ex is CFException) { + MessageBox.Show($"Cannot open file: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void treeView1_MouseUp(object sender, MouseEventArgs e) { + TreeNode n = treeView1.GetNodeAt(e.X, e.Y); + if (n is null) + { + addStorageStripMenuItem1.Enabled = true; + addStreamToolStripMenuItem.Enabled = true; + importDataStripMenuItem1.Enabled = false; + exportDataToolStripMenuItem.Enabled = false; + removeToolStripMenuItem.Enabled = false; + propertyGrid1.SelectedObject = null; + return; + } + // Get the node under the mouse cursor. // We intercept both left and right mouse clicks // and set the selected TreeNode according. try { - TreeNode n = treeView1.GetNodeAt(e.X, e.Y); - - if (n != null) + if (hexEditor.ByteProvider != null && hexEditor.ByteProvider.HasChanges()) { - if (hexEditor.ByteProvider != null && hexEditor.ByteProvider.HasChanges()) + if (MessageBox.Show("Do you want to save pending changes ?", "Save changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { - if (MessageBox.Show("Do you want to save pending changes ?", "Save changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) - { - hexEditor.ByteProvider.ApplyChanges(); - } + hexEditor.ByteProvider.ApplyChanges(); } + } - treeView1.SelectedNode = n; + treeView1.SelectedNode = n; - // The tag property contains the underlying CFItem. - CFItem target = (CFItem)n.Tag; + // The tag property contains the underlying CFItem. + //CFItem target = (CFItem)n.Tag; - if (target.IsStream) - { - addStorageStripMenuItem1.Enabled = false; - addStreamToolStripMenuItem.Enabled = false; - importDataStripMenuItem1.Enabled = true; - exportDataToolStripMenuItem.Enabled = true; + if (n.Tag is CFStream stream) + { + addStorageStripMenuItem1.Enabled = false; + addStreamToolStripMenuItem.Enabled = false; + importDataStripMenuItem1.Enabled = true; + exportDataToolStripMenuItem.Enabled = true; #if OLE_PROPERTY - dgvUserDefinedProperties.DataSource = null; - dgvOLEProps.DataSource = null; - - if (target.Name == "\u0005SummaryInformation" || target.Name == "\u0005DocumentSummaryInformation") - { - OLEPropertiesContainer c = ((CFStream)target).AsOLEPropertiesContainer(); + dgvUserDefinedProperties.DataSource = null; + dgvOLEProps.DataSource = null; - DataTable ds = new DataTable(); + if (stream.Name == "\u0005SummaryInformation" || stream.Name == "\u0005DocumentSummaryInformation") + { + OLEPropertiesContainer c = stream.AsOLEPropertiesContainer(); - ds.Columns.Add("Name", typeof(string)); - ds.Columns.Add("Type", typeof(string)); - ds.Columns.Add("Value", typeof(string)); + DataTable ds = new DataTable(); + ds.Columns.Add("Name", typeof(string)); + ds.Columns.Add("Type", typeof(string)); + ds.Columns.Add("Value", typeof(string)); - foreach (var p in c.Properties) + foreach (OLEProperty p in c.Properties) + { + if (p.Value is not byte[] && p.Value is IList list) { - if (p.Value.GetType() != typeof(byte[]) && p.Value.GetType().GetInterfaces().Any(t => t == typeof(IList))) - { - for (int h = 0; h < ((IList)p.Value).Count; h++) - { - DataRow dr = ds.NewRow(); - dr.ItemArray = new object[] { p.PropertyName, p.VTType, ((IList)p.Value)[h] }; - ds.Rows.Add(dr); - } - } - else + for (int h = 0; h < list.Count; h++) { DataRow dr = ds.NewRow(); - dr.ItemArray = new object[] { p.PropertyName, p.VTType, p.Value }; + dr.ItemArray = new object[] { p.PropertyName, p.VTType, list[h] }; ds.Rows.Add(dr); } } - - ds.AcceptChanges(); - dgvOLEProps.DataSource = ds; - - if (c.HasUserDefinedProperties) + else { - DataTable ds2 = new DataTable(); + DataRow dr = ds.NewRow(); + dr.ItemArray = new object[] { p.PropertyName, p.VTType, p.Value }; + ds.Rows.Add(dr); + } + } - ds2.Columns.Add("Name", typeof(string)); - ds2.Columns.Add("Type", typeof(string)); - ds2.Columns.Add("Value", typeof(string)); + ds.AcceptChanges(); + dgvOLEProps.DataSource = ds; - foreach (var p in c.UserDefinedProperties.Properties) + if (c.HasUserDefinedProperties) + { + DataTable ds2 = new(); + ds2.Columns.Add("Name", typeof(string)); + ds2.Columns.Add("Type", typeof(string)); + ds2.Columns.Add("Value", typeof(string)); + + foreach (OLEProperty p in c.UserDefinedProperties.Properties) + { + if (p.Value is not byte[] && p.Value is IList list) { - if (p.Value.GetType() != typeof(byte[]) && p.Value.GetType().GetInterfaces().Any(t => t == typeof(IList))) - { - for (int h = 0; h < ((IList)p.Value).Count; h++) - { - DataRow dr = ds2.NewRow(); - dr.ItemArray = new object[] { p.PropertyName, p.VTType, ((IList)p.Value)[h] }; - ds2.Rows.Add(dr); - } - } - else + for (int h = 0; h < list.Count; h++) { DataRow dr = ds2.NewRow(); - dr.ItemArray = new object[] { p.PropertyName, p.VTType, p.Value }; + dr.ItemArray = new object[] { p.PropertyName, p.VTType, list[h] }; ds2.Rows.Add(dr); } } - - ds2.AcceptChanges(); - dgvUserDefinedProperties.DataSource = ds2; + else + { + DataRow dr = ds2.NewRow(); + dr.ItemArray = new object[] { p.PropertyName, p.VTType, p.Value }; + ds2.Rows.Add(dr); + } } + + ds2.AcceptChanges(); + dgvUserDefinedProperties.DataSource = ds2; } -#endif } + + hexEditor.ByteProvider = new StreamDataProvider(stream); +#endif } else { - addStorageStripMenuItem1.Enabled = true; - addStreamToolStripMenuItem.Enabled = true; - importDataStripMenuItem1.Enabled = false; - exportDataToolStripMenuItem.Enabled = false; + hexEditor.ByteProvider = null; } - if (n != null) - propertyGrid1.SelectedObject = n.Tag; - - if (n != null) - { - CFStream targetStream = n.Tag as CFStream; - if (targetStream != null) - { - hexEditor.ByteProvider = new StreamDataProvider(targetStream); - } - else - { - hexEditor.ByteProvider = null; - } - } + propertyGrid1.SelectedObject = n.Tag; } catch (Exception ex) { cf?.Close(); - fs?.Close(); - cf = null; + + fs?.Close(); fs = null; treeView1.Nodes.Clear(); fileNameLabel.Text = string.Empty; - MessageBox.Show("Internal error: " + ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show($"Internal error: {ex.Message}", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -524,12 +484,11 @@ void hexEditor_ByteProviderChanged(object sender, EventArgs e) private void closeStripMenuItem1_Click(object sender, EventArgs e) { - if (hexEditor.ByteProvider != null && hexEditor.ByteProvider.HasChanges()) + if (hexEditor.ByteProvider != null + && hexEditor.ByteProvider.HasChanges() + && MessageBox.Show("Do you want to save pending changes?", "Save changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { - if (MessageBox.Show("Do you want to save pending changes ?", "Save changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) - { - hexEditor.ByteProvider.ApplyChanges(); - } + hexEditor.ByteProvider.ApplyChanges(); } CloseCurrentFile(); @@ -537,10 +496,8 @@ private void closeStripMenuItem1_Click(object sender, EventArgs e) private void preferencesToolStripMenuItem_Click(object sender, EventArgs e) { - using (var pref = new PreferencesForm()) - { - pref.ShowDialog(); - } + using PreferencesForm pref = new(); + pref.ShowDialog(); } } } diff --git a/sources/Structured Storage Explorer/StructuredStorageExplorer.csproj b/sources/Structured Storage Explorer/StructuredStorageExplorer.csproj index d75bc1f3..ad7cf6b6 100644 --- a/sources/Structured Storage Explorer/StructuredStorageExplorer.csproj +++ b/sources/Structured Storage Explorer/StructuredStorageExplorer.csproj @@ -37,19 +37,12 @@ - - - False - lib\Be.Windows.Forms.HexBox.dll - - - @@ -68,9 +61,6 @@ true - - - @@ -78,4 +68,7 @@ + + + \ No newline at end of file diff --git a/sources/Structured Storage Explorer/lib/Be.Windows.Forms.HexBox.dll b/sources/Structured Storage Explorer/lib/Be.Windows.Forms.HexBox.dll deleted file mode 100644 index 1d4afbf9..00000000 Binary files a/sources/Structured Storage Explorer/lib/Be.Windows.Forms.HexBox.dll and /dev/null differ