Forum Discussion
How the new Edge will handle file:// URI ?
navijayvargiya sylvainrodrigue narutards rakova17 jpochedley
So sorry everyone! For some reason, I haven't been getting updates to replies/mentions in here so I didn't see any your followup mentions/questions.
Here are some bits of pieces of code we cobbled together to make this work...
SETUP INSTALLER
Below is a method inside a WinForms installer app that adds the new "localfile" entry to the registry and sets the associated command to execute the localfile handler executable (further below).
private void makeRegEntries(string handlerInstallationFolder)
{
RegistryKey localfileKey = Registry.ClassesRoot.CreateSubKey("localfile");
// Create three subkeys under HKEY_CLASSES_ROOT\localfile
// The keys are disposed when execution exits the using statement.
using (RegistryKey
shellSubKey = localfileKey.CreateSubKey("shell"),
openSubKey = shellSubKey.CreateSubKey("open"),
commandSubKey = openSubKey.CreateSubKey("command"))
{
// Create data for the localfile key
localfileKey.SetValue("", "URL:localfile");
localfileKey.SetValue("URL Protocol", "");
// Create data for the command subkey
commandSubKey.SetValue("", "\"" + handlerInstallationFolder + "\" %1");
}
localfileKey.Close();
}
LOCALFILE HANDLER
This console app code is what the "localfile" protocol registry command runs. It basically tells the OS to open the parameter passed in on the data parameter.
static void Main(string[] args)
{
Console.WriteLine("Init");
// Skip everything if there's no args
foreach (string argument in args)
{
// Decode the string passed from the browser, and make it just a path.
string formattedArgumentDecoded = System.Net.WebUtility.UrlDecode(argument).Replace(@"localfile://open/?data=", "");
// For case-insensitive security checks
string formattedArgument = formattedArgumentDecoded.ToLower();
Console.WriteLine("Testing: {0}", formattedArgumentDecoded);
// TIP: Add some blacklist checks here for unqualified file extensions etc. if needed.
Process.Start("explorer.exe", formattedArgumentDecoded);
}
Environment.Exit(0);
}
HELPER METHODS
BTW, there are a number of other convenience methods we added too (called inside the installer). For example, in Chrome there's a checkbox that allows the user to save this protocol association. Chrome has this hidden feature for security, but it can be enabled via the Registry. Same goes for IE and Edge/Chromium. These are the methods that set those (hopefully they will be of some use):
private void enableChromeCheckbox()
{
RegistryKey softwareKey = Registry.LocalMachine.CreateSubKey("SOFTWARE");
// Create three subkeys under HKEY_CLASSES_ROOT\localfile
// The keys are disposed when execution exits the using statement.
using (RegistryKey
policiesKey = softwareKey.CreateSubKey("Policies"),
googleSubKey = policiesKey.CreateSubKey("Google"),
chromeSubKey = googleSubKey.CreateSubKey("Chrome"))
{
// Create data for the localfile key
chromeSubKey.SetValue("ExternalProtocolDialogShowAlwaysOpenCheckbox", "1", RegistryValueKind.DWord);
}
softwareKey.Close();
}
private void enableChromiumEdgeCheckbox()
{
RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true);
// Create three subkeys under HKEY_CLASSES_ROOT\localfile
// The keys are disposed when execution exits the using statement.
using (RegistryKey
policiesKey = softwareKey.CreateSubKey("Policies"),
msSubKey = policiesKey.CreateSubKey("Microsoft"),
edgeSubKey = msSubKey.CreateSubKey("Edge"))
{
// Create data for the localfile key
edgeSubKey.SetValue("ExternalProtocolDialogShowAlwaysOpenCheckbox", "1", RegistryValueKind.DWord);
}
softwareKey.Close();
}
private void enableNoAskToOpenHtmlEdgeCheckbox()
{
using (var hklmHandle = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var localFileKey = hklmHandle.CreateSubKey(@"SOFTWARE\Microsoft\Internet Explorer\ProtocolExecute\localfile", writable: true))
{
if (localFileKey.GetValue("WarnOnOpen") == null)
localFileKey.SetValue("WarnOnOpen", "0", RegistryValueKind.DWord);
}
//Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\ProtocolExecute\
}
We did a lot of research on the topic to get this put together.
https://support.shotgunsoftware.com/hc/en-us/articles/219031308-Launching-applications-using-custom-browser-protocols
All the best!
Jeff
Please have patience with me. I am new to all this. My questions are;
- What kind of code is this?
- Should they all be built into the installer as separate files or all strung together into one?
- should i be using some sort of compiler?
- What kind of files will the be? (.txt or .bat or something else?)
I have never written much code other than a few very simple poweshell or cmd scripts. I am sorry have to ask questions to which the rest of the crowd here already know the answers answers. JeffOwens
- BartdudeNov 09, 2020Copper Contributor
aloveland-SVT You will notice that even on a local webserver you can't get a link to local files to work. The use case for most of the people in this discussion is: publishing resources in a public place. With then one of three scenarios:
- Allowing people to start programs from a wepbage. (The least likely/common scenario.)
- Publishing documents that happen to reside on a network share because of other reasons.
- Allowing editors to update the documents on that networked share. (Open - edit - save.)
Typing a link in the browser bar works for these scenarios. That is why it is frustrating that you can't simply click on those links. You have to copy and paste the link to the address bar.
- st___Nov 09, 2020Copper Contributor
aloveland-SVTThat will probably not help what most people want. It only opens a directory listing inside the browser and/or download the file. Most people will be wanting to open the actual file from its destination so it can be updated/saved directly back, or open a folder in explorer so it can be worked with/in.
- aloveland-SVTOct 22, 2020Copper Contributor
I just ran into this issue today and discovered that in place of the file:/// or file:// URIs that a 'file:' or 'file:/' URI seems to work in the new Microsoft Edge, Google Chrome, and Firefox. (e.g. file:C:\Users\Public\Desktop or file:/C:\Users\Public\Desktop )
Hope this helps anyone that is encountering this issue.