C# Clipboard in Console Application
2014-09-20 14:35
288 查看
发现在控制台应用程序中无法获得剪贴板中的数据。
查看msdn有提到
The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.
所以最终的解决方案如下:
查看msdn有提到
The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.
所以最终的解决方案如下:
public static object GetClipboardData() { object ret = null; ThreadStart method = delegate() { System.Windows.Forms.IDataObject dataObject = Clipboard.GetDataObject(); if (dataObject != null && dataObject.GetDataPresent(DataFormats.Text)) { ret = dataObject.GetData(DataFormats.Text); } }; if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA) { Thread thread = new Thread(method); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); } else { method(); } return ret; }
相关文章推荐
- C# Clipboard in Console Application
- Coloring the Console Application (AddingText and Background color in C#)
- Using third-party filters in a video application in C#
- C# Tip: Placing Your C# Application in the System Tray(把应用程序最小化到系统托架中)(zz)
- 控制台打印表格 (print tables in console application)
- Monitoring Clipboard Activity in C#
- C# Console Application
- How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET
- Show Console Window in Windows Application
- [转载]Step By Step - Using Silverlight to Access a WCF Service Hosted In a Console Application
- Embedding Google Earth in a C# Application (转载)
- How to load "CodedUItestmethod" in another "consoleapplication".
- 70 lines to Implement 'Hosting a WCF Service in a Console Application'
- Using a Console in Winform Application
- Console in C#
- Effective testing in console application
- Counting Number Down In Console Application
- Centralised Exception Handling in C# Windows Application
- Step By Step - Using Silverlight to Access a WCF Service Hosted In a Console Application
- Create a Windows Clipboard Monitor in C# using SetClipboardViewer