diff --git a/src/TestConsole/Program.cs b/src/TestConsole/Program.cs index c5642b33..5e72f866 100644 --- a/src/TestConsole/Program.cs +++ b/src/TestConsole/Program.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using TextCopy; class Program @@ -8,6 +9,7 @@ static async Task Main() var text = "Hello World!"; await ClipboardService.SetTextAsync(text); var result = await ClipboardService.GetTextAsync(); + Console.WriteLine(result); if (result == text) { return 0; diff --git a/src/TestConsole/TestConsole.csproj b/src/TestConsole/TestConsole.csproj index b23f93c4..82df2e12 100644 --- a/src/TestConsole/TestConsole.csproj +++ b/src/TestConsole/TestConsole.csproj @@ -3,9 +3,6 @@ net5 Exe - - net461;netcoreapp3.1 - diff --git a/src/TextCopy/LinuxClipboard_2.0.cs b/src/TextCopy/LinuxClipboard_2.0.cs index a456ad36..2e2ca741 100644 --- a/src/TextCopy/LinuxClipboard_2.0.cs +++ b/src/TextCopy/LinuxClipboard_2.0.cs @@ -1,10 +1,18 @@ #if (NETSTANDARD2_0 || NETFRAMEWORK) +using System; using System.IO; using System.Threading; using System.Threading.Tasks; static class LinuxClipboard { + static bool isWsl; + + static LinuxClipboard() + { + isWsl = Environment.GetEnvironmentVariable("WSL_DISTRO_NAME") != null; + } + public static Task SetTextAsync(string text, CancellationToken cancellation) { SetText(text); @@ -18,7 +26,14 @@ public static void SetText(string text) File.WriteAllText(tempFileName, text); try { - BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard"); + if (isWsl) + { + BashRunner.Run($"cat {tempFileName} | clip.exe "); + } + else + { + BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard "); + } } finally { @@ -28,18 +43,23 @@ public static void SetText(string text) public static Task GetTextAsync(CancellationToken cancellation) { - return Task.FromResult(GetText()); + return Task.FromResult(GetText()); } - public static string? GetText() + public static string GetText() { var tempFileName = Path.GetTempFileName(); try { - BashRunner.Run($"xsel -o --clipboard > {tempFileName}"); - var readAllText = File.ReadAllText(tempFileName); - // ReSharper disable once RedundantTypeArgumentsOfMethod - return readAllText; + if (isWsl) + { + BashRunner.Run($"powershell.exe Get-Clipboard > {tempFileName}"); + } + else + { + BashRunner.Run($"xsel -o --clipboard > {tempFileName}"); + } + return File.ReadAllText(tempFileName); } finally { diff --git a/src/TextCopy/LinuxClipboard_2.1.cs b/src/TextCopy/LinuxClipboard_2.1.cs index 62e0ee98..907eab5d 100644 --- a/src/TextCopy/LinuxClipboard_2.1.cs +++ b/src/TextCopy/LinuxClipboard_2.1.cs @@ -1,36 +1,50 @@ #if (NETSTANDARD2_1 || NET5_0) +using System; using System.IO; using System.Threading; using System.Threading.Tasks; static class LinuxClipboard { + static bool isWsl; + + static LinuxClipboard() + { + isWsl = Environment.GetEnvironmentVariable("WSL_DISTRO_NAME") != null; + } + public static async Task SetTextAsync(string text, CancellationToken cancellation) { var tempFileName = Path.GetTempFileName(); await File.WriteAllTextAsync(tempFileName, text, cancellation); - try - { - if (cancellation.IsCancellationRequested) - { - return; - } - BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard "); - } - finally + if (cancellation.IsCancellationRequested) { - File.Delete(tempFileName); + return; } + + InnerSetText(tempFileName); } public static void SetText(string text) { var tempFileName = Path.GetTempFileName(); File.WriteAllText(tempFileName, text); + InnerSetText(tempFileName); + } + + static void InnerSetText(string tempFileName) + { try { - BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard "); + if (isWsl) + { + BashRunner.Run($"cat {tempFileName} | clip.exe "); + } + else + { + BashRunner.Run($"cat {tempFileName} | xsel -i --clipboard "); + } } finally { @@ -43,7 +57,7 @@ public static void SetText(string text) var tempFileName = Path.GetTempFileName(); try { - BashRunner.Run($"xsel -o --clipboard > {tempFileName}"); + InnerGetText(tempFileName); return File.ReadAllText(tempFileName); } finally @@ -57,7 +71,7 @@ public static void SetText(string text) var tempFileName = Path.GetTempFileName(); try { - BashRunner.Run($"xsel -o --clipboard > {tempFileName}"); + InnerGetText(tempFileName); return await File.ReadAllTextAsync(tempFileName, cancellation); } finally @@ -65,5 +79,17 @@ public static void SetText(string text) File.Delete(tempFileName); } } + + static void InnerGetText(string tempFileName) + { + if (isWsl) + { + BashRunner.Run($"powershell.exe Get-Clipboard > {tempFileName}"); + } + else + { + BashRunner.Run($"xsel -o --clipboard > {tempFileName}"); + } + } } #endif \ No newline at end of file