Skip to content

Instantly share code, notes, and snippets.

@pingec
Last active August 29, 2015 14:21
Show Gist options
  • Save pingec/5433c4eb5234dcde9686 to your computer and use it in GitHub Desktop.
Save pingec/5433c4eb5234dcde9686 to your computer and use it in GitHub Desktop.
Publish photos from windows explorer context menu
using System;
using System.Runtime.InteropServices;
namespace WinExplorerPublisher
{
//Short app to publish jpegs to the web by copying to specified folder. Constructed URL is inserted into clipboard.
//Example of registry script to register the program:
//[HKEY_CLASSES_ROOT\jpegfile\shell\Publish to pingec.si\command]
//@="\"C:\\Program Files (x86)\\pingec\\WinExplorerPublisher.exe\" \"%1\" \"D:\\www\\publish\" \"http://pingec.si/publish\""
internal class Program
{
[DllImport("user32.dll")]
internal static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
internal static extern bool CloseClipboard();
[DllImport("user32.dll")]
internal static extern bool SetClipboardData(uint uFormat, IntPtr data);
[STAThread]
private static void Main(string[] args)
{
if (args == null || args.Length != 3)
{
Console.WriteLine(args.Length);
throw new ArgumentException("Incorrect number of arguments. Expecting 3 arguments: SourcePath, DestinationPath, URL");
}
foreach (var arg in args)
{
Console.WriteLine("->" + arg);
}
var sourceFilePath = args[0];
var destFolderPath = args[1];
var url = args[2];
var fileName = System.IO.Path.GetFileName(sourceFilePath);
var destFilePath = System.IO.Path.Combine(destFolderPath, fileName);
System.IO.File.Copy(sourceFilePath, destFilePath);
url = url.TrimEnd('/') + "/" + fileName;
OpenClipboard(IntPtr.Zero);
var ptr = Marshal.StringToHGlobalUni(url);
SetClipboardData(13, ptr);
CloseClipboard();
Marshal.FreeHGlobal(ptr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment