Powered by GeSHi

Source code for paste.cs

(view source code of paste.cs as plain text)

  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4.  
  5. namespace RobvanderWoude
  6. {
  7. 	class Paste
  8. 	{
  9. 		[STAThread]
  10. 		static int Main( string[] args )
  11. 		{
  12. 			string file = string.Empty;
  13. 			bool overwrite = false;
  14. 			StreamWriter stream;
  15.  
  16. 			#region CommandLine
  17.  
  18. 			if ( args.Length > 0 )
  19. 			{
  20. 				if ( args[0][0] == '/' )
  21. 				{
  22. 					return WriteError( string.Empty, 3 );
  23. 				}
  24. 				else
  25. 				{
  26. 					file = args[0];
  27. 				}
  28. 			}
  29. 			if ( args.Length == 2 )
  30. 			{
  31. 				if ( args[1].ToUpper( ).Trim( ) == "/O" )
  32. 				{
  33. 					overwrite = true;
  34. 				}
  35. 				else
  36. 				{
  37. 					return WriteError( "Invalid command line switch", 3 );
  38. 				}
  39. 			}
  40. 			if ( args.Length > 2 )
  41. 			{
  42. 				return WriteError( "Too many command line arguments", 3 );
  43. 			}
  44.  
  45. 			#endregion CommandLine
  46.  
  47. 			try
  48. 			{
  49. 				try
  50. 				{
  51. 					string clipText;
  52. 					if ( Clipboard.ContainsText( ) )
  53. 					{
  54. 						clipText = Clipboard.GetText( );
  55. 					}
  56. 					else
  57. 					{
  58. 						return 1;
  59. 					}
  60. 					if ( string.IsNullOrWhiteSpace( file ) )
  61. 					{
  62. 						Console.Write( clipText );
  63. 					}
  64. 					else
  65. 					{
  66. 						if ( !File.Exists( file ) || overwrite )
  67. 						{
  68. 							stream = new StreamWriter( file );
  69. 						}
  70. 						else
  71. 						{
  72. 							stream = File.AppendText( file );
  73. 						}
  74. 						stream.Write( clipText );
  75. 						stream.Close( );
  76. 					}
  77. 					return 0;
  78. 				}
  79. 				catch ( IOException e )
  80. 				{
  81. 					return WriteError( e.Message, 2 );
  82. 				}
  83. 			}
  84. 			catch ( Exception e )
  85. 			{
  86. 				Console.Error.WriteLine( e.Message, 3 );
  87. 				return 2;
  88. 			}
  89. 		}
  90.  
  91. 		public static int WriteError( string errorMessage, int rc )
  92. 		{
  93. 			/*
  94. 			Paste.exe,  Version 2.00
  95. 			Read text from the clipboard and write to a file or the screen
  96.  
  97. 			Usage:  PASTE  [ textfile  [ /O ] ]
  98.  
  99. 			Where:  textfile   is the optional file to write the text from the clipboard to
  100. 			                   (default: write to Standard Output, i.e. the screen)
  101. 			        /O         tells the program to overwrite textfile if it exists
  102. 			                   (default: append to existing file, create file if it doesn't exist)
  103.  
  104. 			Note:   The program returns the following 'errorlevels':
  105. 			        0    success
  106. 			        1    no text available in clipboard
  107. 			        2    file I/O error
  108. 			        3    command line or unknown error
  109.  
  110. 			Written by Rob van der Woude
  111. 			http://www.robvanderwoude.com
  112. 			*/
  113.  
  114. 			if ( !string.IsNullOrWhiteSpace( errorMessage ) )
  115. 			{
  116. 				Console.Error.WriteLine( );
  117. 				Console.ForegroundColor = ConsoleColor.Red;
  118. 				Console.Error.Write( "{0}ERROR: ", ( rc == 2 ? "I/O " : String.Empty ) );
  119. 				Console.ForegroundColor = ConsoleColor.White;
  120. 				Console.Error.WriteLine( errorMessage );
  121. 				Console.ResetColor( );
  122. 			}
  123. 			Console.Error.WriteLine( );
  124. 			Console.Error.WriteLine( "Paste.exe,  Version 2.00" );
  125. 			Console.Error.WriteLine( "Read text from the clipboard and write to a file or the screen" );
  126. 			Console.Error.WriteLine( );
  127. 			Console.Error.WriteLine( "Usage:  PASTE  [ textfile  [ /O ] ]" );
  128. 			Console.Error.WriteLine( );
  129. 			Console.Error.WriteLine( "Where:  textfile   is the optional file to write the text from the clipboard to" );
  130. 			Console.Error.WriteLine( "                   (default: write to Standard Output, i.e. the screen)" );
  131. 			Console.Error.WriteLine( "        /O         tells the program to overwrite textfile if it exists" );
  132. 			Console.Error.WriteLine( "                   (default: append to existing file, create file if it doesn't exist)" );
  133. 			Console.Error.WriteLine( );
  134. 			Console.Error.WriteLine( "Note:   The program returns the following 'errorlevels':" );
  135. 			Console.Error.WriteLine( "        0    success" );
  136. 			Console.Error.WriteLine( "        1    no text available in clipboard" );
  137. 			Console.Error.WriteLine( "        2    file I/O error" );
  138. 			Console.Error.WriteLine( "        3    command line or unknown error" );
  139. 			Console.Error.WriteLine( );
  140. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  141. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  142. 			return rc;
  143. 		}
  144. 	}
  145. }
  146.