?

Log in

   Journal    Friends    Archive    Profile    Memories
 

Copy path to clipboard - David Airapetyan on C# and .NET

Jun. 26th, 2007 01:44 am Copy path to clipboard

Quite often I need to accomplish a very simple task: get the full path of a file. In Visual Studio, there is a context menu command called "copy file path to the clipboard". Not so in Windows Explorer.

The good news is, adding a new command to the Explorer shell menu is a breeze. Here is a nice little tutorial on how to do it: http://www.codeproject.com/csharp/SimpleContextMenu.asp

But first, we need a program that can copy text to the clipboard. If you don't want to download one than it's pretty much a one-liner in C#:

class Program
{
  [STAThread()]
  static void Main(string[] args)
  {
   if (args.Length > 0)
   {
    Clipboard.SetText(String.Join(" ", args));
   }
   else
  
{
    MessageBox.Show("cc - copies arguments to the clipboard");
   }
  }
}

I have called my program cc.exe and simply copied it into the Windows directory. Putting it all together, here is a registry script that hooks it up:


REGEDIT4

[HKEY_CLASSES_ROOT\*\shell\cc]
@="Copy path to clipboard"

[HKEY_CLASSES_ROOT\*\shell\cc\command]
@="cc \"%L\""

[HKEY_CLASSES_ROOT\Directory\shell\cc]
@="Copy path to clipboard"

[HKEY_CLASSES_ROOT\Directory\shell\cc\command]
@="cc \"%L\""



Now the new command is available anywhere in the Explorer context menu!

Update: based on a request, the entire program is available as an MSI setup package for convenience: http://www.davidair.com/misc/SetupCC.msi

6 comments - Leave a commentPrevious Entry Share

Comments:

From:cocktailbuilder
Date:June 26th, 2007 08:22 pm (UTC)
(Link)
Can you put together an MSI to install the program and set the registry keys? The community will love you forever :)
From:csharpcoder
Date:June 27th, 2007 02:24 am (UTC)
(Link)
Fine, Mr Lazybones, fine! But you'll have to dogfood it, though! :)

Enjoy:
http://www.davidair.com/misc/SetupCC.msi
From:cocktailbuilder
Date:June 27th, 2007 07:11 am (UTC)
(Link)
I ABSOLUTELY LOVE THIS APPLICATION! It's going into my personal toolbox!

(Just so you know, it contains very few tools... odd, XML Notepad... barely anything else).

AWESOME! Thanks, man!
From:csharpcoder
Date:June 27th, 2007 03:18 pm (UTC)
(Link)
Sweet, you're welcome!
From:(Anonymous)
Date:June 27th, 2007 09:47 pm (UTC)
(Link)
Spasibo, ya tozhe ustanovil.
Pravda, ya dumayu, kogda ya zahochu ustanovit' eto delo na drugom komputere, to nado budet zanovo naiti etot thread. Voobshem mnogo gemora.
Mozhet ti sozdash kakoi-nibud' repository dlya vsyakih toolov, examplov, etc.

Carlos
From:csharpcoder
Date:June 28th, 2007 03:05 pm (UTC)
(Link)
Мне сейчас тяжело менять свой сайт, так, как он сделан в дурацком Паблишере. Мне надо будет сделать ре-дизайн, и тогда я добавлю секцию программ.