[tags]firefox,file link,share,local link[/tags]

Update 04-12-2007: please see my latest blog entry on handling of file links in Firefox (and other protocols) here.

In Internet Explorer you can link to a file or folder on your own harddrive (local pages) or network share using anchor html code like … href=”\serversharefolder” …

Due to security restrictions in Firefox and (as far as I know) all other browsers except IE, such a link will not work.

The best solution I found was to add a handler for smb links in the Windows registry, which invokes a small program that opens the correct link in Windows Explorer.

The fix consist of two files:

  1. smb.reg contains registry settings so that the system, including all browsers ie. Firefox, IE and others, recognize smb links such as smb://\serversharefolder
  2. smb.cs is the source code for smb.exe. When called it will start Windows Explorer on the requested file. When you click a link in your browser the href will be url encoded. The code handles links containing url-encoded characters such as the danish æ,ø and å. Failure to decode such characters will prevent Windows Explorer in finding the requested folder.

smb.reg:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOTsmb]
@=”URL:smb Protocol”
“URL Protocol”=””

[HKEY_CLASSES_ROOTsmbshellopencommand]
@=””smb.exe” “%1″”

smb.cs:

using System;

/// <summary>
/// Takes a smb:// link and starts Windows Explorer in the folder
/// which the link points to.
/// </summary>
public class smb
{
public static void Main(string[] args)
{
if (args.Length == 0)
{
Console.Write(“No destination given”);
return;
}

string dest = args[0].Replace(“smb://”,string.Empty);
if (dest.EndsWith(“/”)) // explorer prefer if paths to folders end with or nothing
dest = dest.Substring(0, dest.Length – 1);

// allow smb://server/share style links
dest = dest.Replace(‘/’, ‘\’);

// rescue some non-conformant URL’s
if(!dest.StartsWith(“\\”))
dest = “\\” + dest;

// since the link probably came from a browser we need to restore
// non-english characters such as æ,ø and å from their html-encoded version

dest = System.Web.HttpUtility.UrlDecode(dest);

if(System.IO.Directory.Exists(dest)){
// use explorer to start folders,
System.Diagnostics.Process.Start(“explorer.exe”, dest);
}
else
{
// files are launched without a program. this assumes proper handlers are set up
System.Diagnostics.Process.Start(dest);
}
}
}

The registry code can be put into a .reg file and imported by double-clicking it or silently using regedit /s smb.reg.

You can compile smb.cs to obtain smb.exe by putting the above code into a file called smb.cs and then invoke the csharp compiler,csc, on it (my compiler lies here %windir%Microsoft.NETFrameworkv2.0.50727).

To complete the fix you must copy smb.exe to a folder, which is in your path eg %windir%.

If you really trust me you can skip the compilation part and download both files.

Update 09-11-2007: smb.exe will now launch files by executing them directly. Folders are still launched with Windows Explorer.