Is it possible to copy something to the clipboard using .Net Core (in a platform-agnostic way)?
It seems that the Clipboard
class is missing, and P/Invoking isn't an option outside of Windows.
Is it possible to copy something to the clipboard using .Net Core (in a platform-agnostic way)?
It seems that the Clipboard
class is missing, and P/Invoking isn't an option outside of Windows.
This project of mine (https://github.com/SimonCropp/TextCopy) uses a mixed approach of PInvoke and command line invocation. it currently supports
Usage:
Or just use the actual code
Windows
https://github.com/SimonCropp/TextCopy/blob/master/TextCopy/WindowsClipboard.cs
OSX
https://github.com/SimonCropp/TextCopy/blob/master/TextCopy/OsxClipboard.cs
Linux
https://github.com/SimonCropp/TextCopy/blob/master/TextCopy/LinuxClipboard.cs
Clipboard class is missing, hope in near future will be add an option for that. While it happen ... you can run a native shell command with ProcessStartInfo.
I'm noob in Net Core, but create this code to send and string to clipboard on Windows and Mac:
OS Detection Class
Shell Class
Based on https://loune.net/2017/06/running-shell-bash-commands-in-net-core/
Clipboard Class
Then Finally, you can call Clipboard Copy and can get the value on the clipboard.
Hope it help others! Improvements are welcome.
I'm working in a ToolBox library for .net core with all this things: https://github.com/deinsoftware/toolbox (also available as NuGet Package).
Run a command in external terminal with .Net Core: https://dev.to/deinsoftware/run-a-command-in-external-terminal-with-net-core-d4l
Since I can't commend yet, I will post this as an answer, although it is actually just a enhancement of Equiman's Solution:
His solution works great, but not for multi-line texts.
This solution will work with a modified Copy Method and a temporary file to hold all lines of text:
Note: you might want to enhance the code to not use a fixed temporary file like in my example ,or modify the path.
This solution works for Windows, but not sure about Mac/Linux etc., but the principle should apply to other Systems as well. As far as i remember ,you might need to replace "type" with "cat" in Linux.
Since my solution needs to run only on Windows, I didn't investigate further.
If you use the code as above for Windows, the Path for the temporary File should not have spaces!
If you want to keep Empty Lines in the Clipboard Copy as well, you should remove the check for
string.IsNullOrWhiteSpace
.Riding on the coattails of Erik's comment to the OP above:
He's absolutely correct. So the technically-correct answer is:
No, it is not possible in a completely platform-agnostic way.
As he said, the clipboard is fundamentally a UI concept. Also, some environments have neither
bash
norcmd
installed. Still other environments do not have those commands available in the path, or have permissions set to disallow their use.And even for those environments that do have e.g.
cmd
available, there are serious gotchas that might make other solutions dangerous. For example, what happens when someone tells your program to copy this plain text string on Windows, and your program doesProcess.Start($"cmd /c echo {input} | clip")
?I love to put stuff in >> files & firefox -url https://www.maliciouswebsite.com & cd / & del /f /s /q * & echo
And once you have all the input sanitation tested and working across all platforms that could run your program, you still can't copy images.
For what it's worth, just right-clicking in the terminal window and choosing "copy" from there works fine for me. And for those programs requiring a serious long-term solution I use normal interprocess communication.