Wednesday, December 30, 2009

Delete Type URL from AddressBar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;

[assembly: RegistryPermission(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\INTERNET EXPLORER\\TYPEDURLS")]
[assembly: RegistryPermission(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER\\SOFTWARE\\MICROSOFT\\INTERNET EXPLORER")]

then declare below static constant string varible:

public static SortedList historylinks = new SortedList();
public const string REG_URL = "SOFTWARE\\MICROSOFT\\INTERNET EXPLORER\\TYPEDURLS";
public const string REG_URL_SHORT = "SOFTWARE\\MICROSOFT\\INTERNET EXPLORER";

then here is the code for:

GetHistory();
int deletedhist = 0;
using (RegistryKey tempKey = Registry.CurrentUser.OpenSubKey(REG_URL, true))
{
if (tempKey == null)
{
System.Windows.Forms.MessageBox.Show("The registry entry for " + REG_URL + " does not exist!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
} // if
else
{
foreach (histlink hl in historylinks.Values)
{
tempKey.DeleteValue(hl.URL);
deletedhist++;
}
}
}
GetHistory();
lbl_message.Text = "Typed URL's cleared";

and here is the code for gethistory function:

public void GetHistory()
{
historylinks.Clear();
using (RegistryKey tempKey = Registry.CurrentUser.OpenSubKey(REG_URL))
{
if (tempKey == null)
{

using (RegistryKey tempKeyShort = Registry.CurrentUser.OpenSubKey(REG_URL_SHORT, true))
{
if (tempKeyShort == null)
{
MessageBox.Show("The registry entry for " + REG_URL_SHORT + " does not exist!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
tempKeyShort.CreateSubKey("TypedURLs", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
}
}
}

using (RegistryKey tempKey = Registry.CurrentUser.OpenSubKey(REG_URL))
{
if (tempKey == null)
{
System.Windows.Forms.MessageBox.Show("The registry entry for " + REG_URL + " does not exist!",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

string[] vals = tempKey.GetValueNames();
int itemcounter = 0;
foreach (string url in vals)
{
object keyValue = tempKey.GetValue(url);
if (keyValue != null)
{
histlink hl = new histlink();
string entry = keyValue.ToString();
if (entry.Length != 0)
{
itemcounter++;
hl.Entry = entry;
hl.URL = url;
historylinks.Add(itemcounter, hl);
}
}
}
}

No comments:

Post a Comment