E-Mail:
Author Avatar

Firefox file links

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=”\\server\share\folder” …

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://\\server\share\folder
  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_ROOT\smb]
@=”URL:smb Protocol”
“URL Protocol”=”"

[HKEY_CLASSES_ROOT\smb\shell\open\command]
@=”\”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.NET\Framework\v2.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.

Tags: , , ,

7 Comments

Your example is good for creating new protocol handlers, however, their is already an SMB protocol naming convention.

I have been hunting around, trying to find a way to get IE or Firefox on windows to handle fully qualified SMB addresses on Windows, so Intranet links work there as well as on OS X and Linux workstations.

The trouble with your solution is that OS X and SAMBA enabled Unixes accept “\\FileServer.Domain.Local\FileShare\DocumentShare” in the form “smb://fileserver.domain.local/fileshare/documentshare”

I don’t write C# and I think your code would need more work to break out this URL into something that explorer.exe would accept.

Ideally explorer should except such a URL internally. But I think adding an object to the name space would be required for that.

Additionally a URL “smb://fileserver.domain.local/index.html” should load that html page in the current browser. This works on just about any SMB enabled OS except Windows. :( And TBH I don’t know how you could get around that without re-writing Explorer and IE. I can’t think of any protocol handler or name space extension that does anything similar… unless it be an ActiveX control based browser extension… still not sure you could get all the way without hacking. :s

is there an extension to let firefox act as a replacement to windows explorer and if not why not?

I like the idea… though I can’t get it to work. I can run the program fine from the command line (”smb.exe \\server” opens the folder). I have smb.exe in my path. I get Firefox to pop up the warning “An application must be launched to handle smb: links.” But when I click on the “Launch Application” button, nothing happens.

FYI, Firefox 2.0.0.7 on Vista. I also changed the code to pop up a MsgBox (which, again, works from the command line) and can tell that the smb.exe never gets launched by Firefox…

Thoughts?

And also some tweaks for when it does work… I added the following line near the end of the main method for my own preference so I can do “smb://server/Share”:
dest = dest.Replace(’/', ‘\\’);

Also, FYI, when copying and pasting from the webpage, Firefox converts the quote (”) into “ or ” (curly quotes), which in turn keeps csc from compiling the code. It’s an easy fix, but I thought I’d mention it in case anyone else has the same problem (yes, Notepad in Vista uses curly quotes — tres annoying).

Hi Jason

Thank you for your post. I don’t think I ever tried the program in Vista. The first thing I thought about when I read your description of the problem was to insert a popup message in the program, but you tried that already.

This could possibly have something to do with Vista user account control — maybe there is some kind of silent prompt when smb.exe is launched.

Good point about the conversion to backslash, that will reduce the length of the links.

BTW, Jason. Did you try the SMB links in Internet Explorer?

Looking again for a solution to the same problem… seems like Jason has solved my main gripe… have you included that in your update? If so that may be my best solution right now.

As to Brians’ question… hmm, well the URL “file://C|Documents%20and%2Settings” on a standard install of 2K/2K3/XP install should take you to the local user profile folder on any browser.

You can, (scarily easily as many hoax spyware ads have shown) embed the Explorer folder view for any particular directory in to a webpage… and that will work on any Windows machine with IE4+… basically win95 SP1 and newer.

The idea of making a plugin for FF that would do it without embedding IE is rather more tricky. Explorer replacements are usually either quite expensive, rather under functional, or oft times both. The reason is that many programs require close integration with the Windows default GUI Shell (explorer.exe) and it’s numerous (tho with the recent disclosure agreement reducing) undocumented features.

Features typically lacking include, but are not limited to, Context menu extensions, tool bar extensions (band objects), DDE linkage, parameter passing, clipboard handling for file linkage, drag and drop compatibility, image preview extensions, web view, unusual executables (VBS, WSH, HTX et el) etc.

If you are happy enough to just root through the filesystem and have explorer run anything you click on after “downloading” it from your own hard drive… the “file://” URL form is your friend… anything more than that is really… really beyond the abilities of a plugin system based on Java that is supposed to be largely platform independent.

Thanks.
The solution seems to work well for me. However, I wish there was a easier way to do it (something that worked out of the box).

I’m using Firefox w/ WindowsXP and I’d like to be able to easily have people use share drives to place content for our new wiki (most of our existing content is already located on share drives). Right now every PC would need to be setup with this update before it would work.

Windows Internet Explorer can do the same thing with file://. It seems to me that firefox should just give a setting (that could be turned on/off for security) that allows the same behavior and all would be well.

Thanks,
Sten

What Do You Think?

 


Anti-Spam Image

Want to Start a Blog Here for Free?

Are you an expert in one subject or another? If your goal is to help others and dispense hard-earned information back to the community, stake a claim on your very own Lockergnome blog today! You can write about anything - no matter the topic. Sign-up to start blogging!

Author Avatar
General - May 8, 2007

No more Vista