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 is
Since I can't comment 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:
public static void Copy(string val)
{
string[] lines = val.Split('\n');
if (lines.Length == 1)
$"echo {val} | clip".Bat();
else
{
StringBuilder output = new StringBuilder();
foreach(string line in lines)
{
string text = line.Trim();
if (!string.IsNullOrWhiteSpace(text))
{
output.AppendLine(text);
}
}
string tempFile = @"D:\tempClipboard.txt";
File.WriteAllText(tempFile, output.ToString());
$"type { tempFile } | clip".Bat();
}
}
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
.