简体   繁体   中英

How to associate icon with custom application(registered with a powershell script)

I use cygwin version of gvim to edit files in windows, for that I created a bat script that opens a file with cygwin version of gvim(by converting the path to cygwin format). I also wrote a small powershell script to register this bat script with windows explorer so I can associate file extensions using the 'open with' context menu. Here is the script:

$ErrorActionPreference = "Stop"
$classes="hkcu:\software\classes"
$appid="cygwin.gvim"
$apps="$classes\applications"
$cmd='...SOMEDIRECTORY...\edit-gvim.bat'
$friendlyname='gVim (Cygwin)'
$icon='...ANOTHERDIRECTORY...\vim.ico'
$filetypes=@(".txt", ".ps1", ".sh", ".py", ".cs", ".cpp", ".c", ".rb",
    ".zsh", ".bash", ".vbox", ".xml", ".yml", ".yaml", ".bat")

if (test-path "$apps\$appid") {
  # cleanup
  remove-item -recurse "$apps\$appid"
}

# register open commands to know filetypes
new-item -path "$apps\$appid\shell\open\command" -value "$cmd `"%1`"" -force

# add a context menu item(edit with gVim) to every file in windows explorer
new-item -path "$classes\*\shell\Edit with $friendlyname\command" -value "$cmd `"%1`"" -force

# friendly name for the 'open with' dialog
new-itemproperty -path "$apps\$appid\shell\open" -name 'FriendlyAppName' -value $friendlyname

# register the icon
# FIXME this has no effect 
new-item -path "$apps\$appid\DefaultIcon" -value $icon -type expandstring

# register supported file extensions
new-item -path "$apps\$appid\SupportedTypes"
foreach ($ext in $filetypes) {
  new-itemproperty -path "$apps\$appid\SupportedTypes" -name $ext -PropertyType string -value ''
}

Everything works except the line below the 'FIXME' comment, which aparently has no effect. Instead of seeing my provided icon with applications associated with gvim, I see windows default icon for unknown file types. What am I missing here?

Here are some resources I used to write this script:

I can make a mistake, but as far as I understand the syntax of DefaultIcon is :

"Full path to a file,ordinal"

This is usefull when you want to point to a resource in an EXE or a DLL

C:\Program Files (x86)\PowerGUI\ScriptEditor.exe,1

But you have to keep the same syntax if you want to point to an icon file :

[HKEY_CLASSES_ROOT\AcroExch.acrobatsecuritysettings.1\DefaultIcon]
@="C:\\Windows\\Installer\\{AC76BA86-7AD7-1036-7B44-A95000000001}\\PDFFile_8.ico,0"

So, in your case, I would try :

$icon='...ANOTHERDIRECTORY...\vim.ico,0'

or

$icon='...ANOTHERDIRECTORY...\\vim.ico,0'

Edited

Don't forget to restart explroer.exe to see the new icon.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM