In previous post we stated extension for creating url shortcuts.
Now it is time to create actual file explorer menu items and get the basics working.
First let’s create small class what will deal with needed registry entries. Creating contect menu items is quite simple, just two entries in registry.
using Microsoft.Win32; using System.Windows.Forms; namespace UrlExtension { public class UriExtensionUtils { public static void AddOption_ContextMenu() { RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Directory\\Background\\Shell", true); RegistryKey newkey = _key.CreateSubKey("Add URL Shortcut"); RegistryKey subNewkey = newkey.CreateSubKey("command"); string appDirectory = Application.ExecutablePath.Replace("\\", "\\\\"); subNewkey.SetValue("", appDirectory); subNewkey.Close(); newkey.Close(); _key.Close(); } public static void RemoveOption_ContextMenu() { RegistryKey _key = Registry.ClassesRoot.OpenSubKey("Directory\\Background\\Shell\\", true); _key.DeleteSubKey("Add URL Shortcut"); _key.Close(); } } }
To handle registration and unregistration two buttons are added to screen :
private void butonInstall_Click(object sender, EventArgs e) { UriExtensionUtils.AddOption_ContextMenu(); } private void buttonUninstall_Click(object sender, EventArgs e) { UriExtensionUtils.RemoveOption_ContextMenu(); }
From last time some changes were made to page title handling:
private void buttonTest_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textUrl.Text)) return; // Create an instance of WebClient WebClient client = new WebClientWithTimeout(); try { string html = client.DownloadString(new Uri(textUrl.Text)); Match m = Regex.Match(html, @"<title>\s*(.+?)\s*</title>", RegexOptions.IgnoreCase); string title; title = m.Value.Replace("<title>", "").Replace("</title>", "").Trim(); textTitle.Text = title; if (checkBoxWithIcon.Checked) GetFavouriteIcon(new Uri(textUrl.Text)); } catch (Exception ex) { MessageBox.Show("Could not complete. Error:" + ex.Message); } }
Also url validation now handles also https links:
private bool CheckURLValid(string url) { Uri uriResult; bool result = Uri.TryCreate(url, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttp; if (!result) result = Uri.TryCreate(url, UriKind.Absolute, out uriResult) && uriResult.Scheme == Uri.UriSchemeHttps; return result; }
Now let’s add title and icon fetching on form load :
private void UrlForm_Load(object sender, EventArgs e) { pictureIcon.Tag = null; string url = Clipboard.GetText(); if (string.IsNullOrEmpty(url)) return; bool isUri = Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute); if (isUri) isUri = CheckURLValid(url); if (isUri) { textUrl.Text = url; // Seems we have url load title and icon buttonTest_Click(sender, null); } }
Also shortcut creation needs facelift. Instead of asking location it is known now :
string currentDir = Directory.GetCurrentDirectory();
It makes sense to close application after shortcut creation, so final statement is Close();
private void buttonAdd_Click(object sender, EventArgs e) { string currentDir = Directory.GetCurrentDirectory(); if (!string.IsNullOrWhiteSpace(currentDir)) { if (pictureIcon.Tag != null) CreateUrlShortcut(textTitle.Text, textUrl.Text, currentDir, pictureIcon.Tag.ToString()); else CreateUrlShortcut(textTitle.Text, textUrl.Text, currentDir); } Close(); }
And we are ready to make full functionality test. After running program and installing shortcut we have :
Clicking on menu will bring up screen with filled info
And clicking on Add Shortcut will result like this
Some more error handlers, command line keys to install/uninstall and application would be more-less ok,
Useful links
How to Add Any Application Shortcut to Windows Explorer’s Context Menu