Is there an easier way to use the ctrl + v (paste) functions of Windows in a C # Console application?

We create the console application with the command interpreter. To simplify the task, I needed to add support for reading the clipboard when I pressed ctrl + v . When I press ctrl + v, I see the ^ V character in the console, so Im replaces that character with the clipboard text. After some googling, Ive found out that the buffer can be accessed by System.Windows.Forms.Clipboard.GetText ().

My question is: Is there a better solution for adding clipboard support to a console application? Perhaps without using System.Windows.Forms.Clipboard? Maybe calling interop can do the trick?

One of the drawbacks of this solution is that the clipboard only works when the thread is defined as [STAThread]. It would be much better if I could get rid of the symbol ^ V.

This is the code for the current solution:

using System; using System.Threading; using System.Windows.Forms; namespace ConsoleApplication1 { class Program { public static readonly string ClipboardChar = Convert.ToChar(22).ToString(); [STAThread] static void Main(string[] args) { Console.Write("Do some pastin': "); //read Console.ForegroundColor = ConsoleColor.White; string result = Console.ReadLine(); Console.ResetColor(); //read keyboard if (result.Contains(ClipboardChar)) { result = result.Replace(ClipboardChar, Clipboard.GetText()); } //write result Console.WriteLine("\nResult: "); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(result); Console.ResetColor(); Console.WriteLine("\nPress any key to continue..."); Console.ReadKey(); } } } 
+4
source share
2 answers

You can use P / Invoke for this. Please treat the sample code as proof of concept since it was quickly assembled and assembled. I took a few freedoms - for example, my prototype for GlobalLock returns string , although the Win API does return LPVOID .

 using System; using System.Runtime.InteropServices; namespace clipboard { class Program { public static void Main(string[] args) { ConsoleKeyInfo ki = Console.ReadKey( true ); if( ( ki.Key == ConsoleKey.V ) && ( ki.Modifiers == ConsoleModifiers.Control ) ) { Console.WriteLine( "Ctrl+V pressed" ); string s = ClipBoard.PasteTextFromClipboard(); Console.WriteLine( s ); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } class ClipBoard { [DllImport("user32.dll", SetLastError = true)] private static extern Int32 IsClipboardFormatAvailable( uint format ); [DllImport("user32.dll", SetLastError = true)] private static extern Int32 OpenClipboard( IntPtr hWndNewOwner ); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr GetClipboardData( uint uFormat ); [DllImport("user32.dll", SetLastError = true)] private static extern Int32 CloseClipboard(); [DllImport("kernel32.dll", SetLastError = true)] private static extern Int32 GlobalLock( IntPtr hMem ); [DllImport("kernel32.dll", SetLastError = true)] private static extern Int32 GlobalUnlock( IntPtr hMem ); [DllImport("kernel32.dll")] public static extern UIntPtr GlobalSize(IntPtr hMem); const uint CF_TEXT = 1; public static string PasteTextFromClipboard() { string result = ""; if( IsClipboardFormatAvailable( CF_TEXT ) == 0 ) { return result; } if( OpenClipboard((IntPtr)0) == 0 ) { return result; } IntPtr hglb = GetClipboardData(CF_TEXT); if( hglb != (IntPtr)0 ) { UIntPtr size = GlobalSize(hglb); IntPtr s = GlobalLock(hglb); byte[] buffer = new byte[(int)size]; Marshal.Copy(s, buffer, 0, (int)size); if (s != null) { result = ASCIIEncoding.ASCII.GetString(buffer); GlobalUnlock(hglb); } } CloseClipboard(); return result; } } } 
+8
source

If you click on the icon in the upper left corner of the console application window, you will get "Edit" | Paste option.

+3
source

Source: https://habr.com/ru/post/1335812/


All Articles