I've created a web application that uses local like i.e. file://c:\docs\example.docx Which works fine in IE however modern browsers like chrome and edge wont open local links.
Its a corporate environment so I can't change any settings in the browser but I've managed to update some registry settings. I'm looking into adding a custom protocol.
I've managed to add these registry keys
[HKEY_CURRENT_USER\Software\Classes\MySoftware]
@="URL:MySoftware Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\MySoftware\shell]
[HKEY_CLASSES_ROOT\MySoftware\shell\open]
[HKEY_CLASSES_ROOT\MySoftware\shell\open\command]
@="powershell.exe -Command \"$val='%1'; $val = $val.Substring(10, $val.length - 10).TrimEnd('/').Replace('/','\\'); & 'EXPLORER.EXE' $val\""
This allows urls like
MySoftware://c:\
Which opens up windows explorer on the users machine with c drive open. However it does not seem to work with direct links to files. E.g.
MySoftware://c:\documents\mydoc.docx
This should open c:\documents\mydocs.docx using word.
At the moment the power shell script in the registry wont open the document. I'm assuming that's because the registry entry calls power shell and executes explorer.exe passing in the parameter from the URL.
I've found invoke-item as a command in power shell which works if I execute
powershell.exe -Command invoke-item c:\documents\mydocs.docx
I can't get the command in the registry right, I tried replacing the explorer.exe part with invoke-item but it didn't work. I don't know anything about power shell. What should it be to call power shell and execute invoke-item passing in the file name?
I managed to get it working using.
powershell -command Add-Type -AssemblyName System.Web; $val='%1'; $val = [System.Web.HttpUtility]::UrlDecode($val.Substring(13)).TrimEnd('/'); invoke-item $val;
I used this to find out what's passed to the command, which is the whole URL e.g. "mysoftware://C:%5cdocument.docx/"
powershell -command echo %1; read-host "test";
Changed to this to get only the path and drop the URL pre-fix e.g. "C:%5cdocument.docx/"
powershell -command $val='%1'; $val = $val.Substring(13); echo $val; read-host "test";
Need to decode the path from the URL e.g. "C:\document.docx/"
powershell -command Add-Type -AssemblyName System.Web; $val='%1'; $val = [System.Web.HttpUtility]::UrlDecode($val.Substring(13)); echo $val; read-host "test"
There is an extra '/' on the end of the path so remove that e.g. "C:\document.docx"
powershell -command Add-Type -AssemblyName System.Web; $val='%1'; $val = [System.Web.HttpUtility]::UrlDecode($val.Substring(13)).TrimEnd('/'); echo $val; read-host "test"
Change to invoke item and remove the pause
powershell -command Add-Type -AssemblyName System.Web; $val='%1'; $val = [System.Web.HttpUtility]::UrlDecode($val.Substring(13)).TrimEnd('/'); invoke-item $val;