How to get filepath of selected file in windows explorer context-menu in C#? -
i've created music player using visual studio 2012 windowsform c#, want play/add songs windows explorer other player(windows media player,winamp,mpcstar,vlc...) does! think wouldn't real hard! of these programs simple!
so example: select 3 songs in directory in explorer , right click on them , select "play " , should use application add function added playlist , start playing! if user press enter key should operation!
if user select "add playlist" songs should added playlist (not replace previous playlist songs)
i don't want create program need answer know how can paths of selected files windows explorer context-menu!
*** want selected files path not single file!
** update: found solution! posted answer below! hope helps others :)
ok i've got solution :)
this link helped me paths of selected files in explorer clicking on context-menu item: .net shell extensions - shell context menus
real easy :)
here's steps:
1) download thee sharpshell library>>
download 'sharpshell library' zip file @ top of article , add reference downloaded sharpshell.dll file.
or can download via nuget:
if have nuget installed, quick search sharpshell , install directly - or package details @ https://www.nuget.org/packages/sharpshell.
add following references:
system.windows.forms system.drawing
use these @ top of code:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using sharpshell; using sharpshell.sharpcontextmenu; using system.windows.forms; using system.io; using system.runtime.interopservices; using sharpshell.attributes;
derive class sharpcontextmenu
right click on sharpcontextmenu part of line , choose implement abstract class
.
canshowmenu
this function called determine whether should show context menu extension given set of files. files user has selected in property selecteditempaths
. can check these file paths see whether want show menu. if menu should shown, return true
. if not, return false
.
createmenu
this function called create context menu. standard winforms contextmenustrip
need return.
here's whole namespace sourcecode:
namespace countlinesextension { [comvisible(true)] [comserverassociation(associationtype.classofextension, ".txt")] public class class1 : sharpcontextmenu { protected override bool canshowmenu() { // show menu. return true; //throw new notimplementedexception(); } protected override contextmenustrip createmenu() { // create menu strip. var menu = new contextmenustrip(); // create 'count lines' item. var itemcountlines = new toolstripmenuitem { text = "count lines" }; // when click, we'll call 'countlines' function. itemcountlines.click += (sender, args) => countlines(); // add item context menu. menu.items.add(itemcountlines); // return menu. return menu; //throw new notimplementedexception(); } private void countlines() { // builder output. var builder = new stringbuilder(); // go through each file. foreach (var filepath in selecteditempaths) { // count lines. builder.appendline(string.format("{0} - {1} lines", path.getfilename(filepath), file.readalllines(filepath).length)); } // show ouput. messagebox.show(builder.tostring()); } } }
next, must give assembly strong name. there ways around requirement, best approach take. this, right click on project , choose 'properties'. go 'signing'. choose 'sign assembly', specify 'new' key , choose key name. can password protect key if want to, not required
now install , register shell extension: regasm tool
you can use tool 'regasm' install , register shell extension. when using regasm, shell extension installed registry (i.e. class id of com server put in com server classes section , associated path actual server file), register associations.
the server manager tool
the server manager tool preferred approach installing/uninstalling , registering/unregistering, @ least during development, because lets install , register separate steps. let specify whether you're installing/uninstalling etc in 32 bit or 64 bit mode.
it whole sample sourcecode. can add number of context-menu items, function,any fileextension,etc.
for example i'm gonna use '.mp3' fileextensions , change countlines function function send selecteditempaths
playlist , rest of operations.
hope others too!
Comments
Post a Comment