c# - Select a file for renaming in SharpShell context menu -
i'm using sharpshell write tiny new shell context menu item copies selected files new subfolder, prompts user directory's new name.
searching stackoverflow, found this answer. however, i'd same in sharpshell.
i somehow have fire svsi_edit
@ it, can find buried deep in sharpshell.interop
, i'm not sure how of works. can't find documentation or code samples whatsoever.
(edit: think finding out how pidl
file name start, maybe don't need @ all?)
you can start creating project sharpshell register new shell context menu in tutorial.
here, have define class implementing sharpcontextmenu
. simplicity create menu filetype , show it:
[comvisible(true)] [comserverassociation(associationtype.allfiles)] public class copyfilesextension : sharpcontextmenu { protected override bool canshowmenu() { return true; } protected override contextmenustrip createmenu() { var menu = new contextmenustrip(); var copyfiles = new toolstripmenuitem { text = "copy files folder..." }; copyfiles.click += (sender, args) => copyfiles(); menu.items.add(copyfiles); return menu; } private void copyfiles() { ... } }
but i'm sure you've done this, problem here implement copyfiles()
method.
one way showing dialog asking name of folder, this:
then, implement copyfiles()
so:
private void copyfiles() { using (var dialog = new copyfiledialog()) { if (dialog.showdialog() == dialogresult.ok) { var folder = path.getdirectoryname(selecteditempaths.first()); var newfolder = path.combine(folder, dialog.foldername); directory.createdirectory(newfolder); foreach (var path in selecteditempaths) { var newpath = path.combine(newfolder, path.getfilename(path)); file.move(path, newpath); } } } }
in above code, asked name of folder, create folder , move selected files folder.
however, if want using rename command in windows explorer can start importing needed win32 functions:
class win32 { [dllimport("shell32.dll", charset = charset.unicode)] public static extern intptr ilcreatefrompath([in, marshalas(unmanagedtype.lpwstr)] string pszpath); [dllimport("shell32.dll")] public static extern int shopenfolderandselectitems(intptr pidlfolder, uint cidl, intptr[] apidl, int dwflags); [dllimport("shell32.dll")] public static extern void ilfree(intptr pidl); }
ilcreatefrompath
allows pidl filename.shopenfolderandselectitems
allow select file , send rename command.ilfree
frees unmanagedpidl
created.
with these win32 functions can defines copyfiles()
follows:
private void copyfiles() { var folder = path.getdirectoryname(selecteditempaths.first()); var newfolder = path.combine(folder, "new folder"); directory.createdirectory(newfolder); foreach (var path in selecteditempaths) { var newpath = path.combine(newfolder, path.getfilename(path)); file.move(path, newpath); } renameinexplorer(newfolder); } private static void renameinexplorer(string itempath) { intptr folder = win32.ilcreatefrompath(path.getdirectoryname(itempath)); intptr file = win32.ilcreatefrompath(itempath); try { win32.shopenfolderandselectitems(folder, 1, new[] { file }, 1); } { win32.ilfree(folder); win32.ilfree(file); } }
we can't use sharpshell.interop.shell32
since method available in class shellexecuteex()
used launch new processes.
Comments
Post a Comment