c# – Windows File Explorer extension for creating url shortcuts – Part 1

By | 15. November 2016

I like to save interesting websites as file system shortcuts in OneDrive directiory. It works nice using New->Shortcut in file explorer, but there is room for improvement.

In first part we will look how to fetch website info and create url shortcut and in second will convert prepared code to file explorer extension.

Development is easier using standard windows application. On main form is just one button to call another form what will do all the magic.

The second form is already more complex

On form load clipboard is checked and if there is valid internet url then Url TextBox is filled

Uri.IsWellFormedUriString alone is not enough as it is true also for  file system paths. Often there is some other text on clipboard so we test it first before Uri.TryCreate but this is not mandatory and can be omitted.

For fetching web page we have inherited WebClient class with timeout option:

Test button is used only for developing and will be removed later. It will do all web page related tasks – downloading web page, finding page title and favorite icon.

Page title will be used for shortcut name and is fetched from webpage using Regex. To get favorite icon needs a bit more effort

GetFavouriteIcon tries to download favicon.ico from website and if not found then from http://www.google.com/s2/favicons?domain={url.Host}

If found then icon content is saved in byte[] iconArray for later use and also onscreen PictureBox is filled with this content. So save actual icon file not converted image format the iconArray will be written to file. Using Image.Save() will convert Icon and it will not be usable as shortcut icon. pictureIcon.Tag is used to save icon name, but sure it can be also recalculated later ob schortcut creation.

Result will be something similar:

Add button will ask for File Directory and create inside it shortcut and icon file

On files creation the valid file name is created as by default page title is used and this my contain invalid charactes.

 

So we click on Add Shortcut button, select folder and will get shortcut

Shortcut file content is actually very simple

[InternetShortcut]
URL=http://stackoverflow.com/
IconIndex = 0
IconFile =C:\temp\Links\stackoverflow.com.ico

Created shortcut will look like this : (shortcut file Stack Overflow.url and icon file stackoverflow.com.ico)

 

Next time we will continue with transforming window application to file explorer extension.

Useful links

 

http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream

Get Root Domain and Favicon from URL in C#

http://www.codeproject.com/Articles/19132/Retrieve-a-Web-Pages-Shortcut-Icon

http://stackoverflow.com/questions/7578857/how-to-check-whether-a-string-is-a-valid-http-url

http://stackoverflow.com/questions/4897655/create-shortcut-on-desktop-c-sharp

http://stackoverflow.com/questions/3825433/c-sharp-remove-invalid-characters-from-filename

 

Continue to Part 2

 

Leave a Reply