C#从windows剪贴板获取并显示文本内容的方法

 更新时间:2015年04月17日 15:07:31   作者:work24  
这篇文章主要介绍了C#从windows剪贴板获取并显示文本内容的方法,涉及C#操作剪贴板的相关技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了C#从windows剪贴板获取并显示文本内容的方法。分享给大家供大家参考。具体如下:

using System;
using System.Windows.Forms;
namespace RobvanderWoude
{
 class Paste
 {
  [STAThread]
  static int Main( string[] args )
  {
   if ( args.Length == 0 )
   {
    try
    {
     if ( Clipboard.ContainsText( ) )
     {
      string clipText = Clipboard.GetText( );
      Console.Write( clipText );
      return 0;
     }
     else
     {
      return 1;
     }
    }
    catch ( Exception e )
    {
     Console.Error.WriteLine( e.Message );
     return 2;
    }
   }
   else
   {
    Console.Error.WriteLine( );
    Console.Error.WriteLine( "Paste.exe, Version 1.01" );
    Console.Error.WriteLine( "Read and display text from the clipboard" );
    Console.Error.WriteLine( );
    Console.Error.WriteLine( "Usage: PASTE" );
    Console.Error.WriteLine( );
    Console.Error.WriteLine( "Note:  The program returns the following 'errorlevels':" );
    Console.Error.WriteLine( " 0 success" );
    Console.Error.WriteLine( " 1 no text available in clipboard" );
    Console.Error.WriteLine( " 2 command line or unknown error" );
    Console.Error.WriteLine( );
    Console.Error.WriteLine( "Written by Rob van der Woude" );
    return 2;
   }
  }
 }
}

希望本文所述对大家的C#程序设计有所帮助。

相关文章

最新评论