2

This question is not for how to open a console app by dragging and dropping a file onto the exe and then accessing the file path as an argument.

Instead, this is for handling the drag/drop of a file onto an already-open console window. If you've ever done this while waiting for input on a Console.ReadLine(), you will know that it will input the dragged/dropped file path into the console window so that you can access it. The problem is that it wants to wrap the path in double quotes which causes problems with a lot of the methods in the IO namespace.

If I could create a single handler to intercept the path before it writes it, I could sanitize away the quotes in a single place before returning the string. Any way to do this?

oscilatingcretin
  • 10,457
  • 39
  • 119
  • 206

1 Answers1

6

No drag+drop support in console mode apps. This works only because conhost.exe implements it, the process that owns the console window. You can't mess with what it does, different process. Fwiw, also the reason that conhost.exe needed to be added to Win7, it was necessary to allow dragging from Explorer to a console app that runs with UAC elevation. Not normally allowed.

The double quotes are added because this was meant to make the command processor easier to use. It needs those double quotes to properly handle paths with embedded spaces. Writing the code to deal with them ought to be straight-forward, String.Replace() gets the job done. Double quotes are never valid in a path string so you don't even have to check if they appear at the start and end.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Just want to clarify that I am talking about dragging files onto an already-open console window, so there wouldn't be any command parsing. What I am doing now is using `.Trim('"')' to remove double quotes whenever my app requests path input, but it's a bit tedious to do every time. So is your answer based on drag/drop onto open console window? – oscilatingcretin Dec 02 '18 at 15:45
  • Yes, it is based on *any* console window. – Hans Passant Dec 02 '18 at 17:18