Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for deltrash.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class DelTrash
  10. 	{
  11. 		static string progver = "1.00";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			bool confirm = false;
  17. 			bool progress = false;
  18. 			RecycleFlags flags = RecycleFlags.RecycleNoSound;
  19. 			string drive = null; // default: All drives
  20. 			string windrive = Path.GetPathRoot( Environment.SystemDirectory );
  21.  
  22.  
  23. 			#region Parse Command Line
  24.  
  25. 			if ( args.Length > 3 )
  26. 			{
  27. 				return ShowHelp( );
  28. 			}
  29.  
  30. 			foreach ( string arg in args )
  31. 			{
  32. 				switch ( arg.ToUpper( ) )
  33. 				{
  34. 					case "/?":
  35. 						return ShowHelp( );
  36. 					case "/C":
  37. 						if ( confirm )
  38. 						{
  39. 							return ShowHelp( "Duplicate command line switch /C" );
  40. 						}
  41. 						confirm = true;
  42. 						break;
  43. 					case "/P":
  44. 						if ( progress )
  45. 						{
  46. 							return ShowHelp( "Duplicate command line switch /P" );
  47. 						}
  48. 						progress = true;
  49. 						break;
  50. 					case "/W":
  51. 						if ( drive != null )
  52. 						{
  53. 							if ( drive == windrive )
  54. 							{
  55. 								return ShowHelp( "Either specify a drive letter or use the /W switch, not both" );
  56. 							}
  57. 							else
  58. 							{
  59. 								return ShowHelp( "Duplicate drive specification {0} and {1}", drive, arg.ToUpper( ) );
  60. 							}
  61. 						}
  62. 						drive = windrive;
  63. 						break;
  64. 					default:
  65. 						bool validdrive = false;
  66. 						DriveInfo[] alldrives = DriveInfo.GetDrives( );
  67. 						foreach ( DriveInfo drvinf in alldrives )
  68. 						{
  69. 							if ( drvinf.Name == arg.ToUpper( ) + "\\" )
  70. 							{
  71. 								validdrive = true;
  72. 								drive = arg.ToUpper( );
  73. 							}
  74. 						}
  75. 						if ( !validdrive )
  76. 						{
  77. 							return ShowHelp( "Invalid command line argument \"{0}\"", arg );
  78. 						}
  79. 						break;
  80. 				}
  81. 			}
  82.  
  83. 			if ( !confirm )
  84. 			{
  85. 				flags |= RecycleFlags.RecycleNoConfirmation;
  86. 			}
  87. 			if ( !progress )
  88. 			{
  89. 				flags |= RecycleFlags.RecycleNoProgressUI;
  90. 			}
  91.  
  92. 			#endregion Parse Command Line
  93.  
  94.  
  95. 			uint result = SHEmptyRecycleBin( IntPtr.Zero, drive, flags );
  96. 			if ( result == 0 )
  97. 			{
  98. 				return 0;
  99. 			}
  100. 			else
  101. 			{
  102. 				return 1;
  103. 			}
  104. 		}
  105.  
  106.  
  107. 		static int ShowHelp( params string[] errmsg )
  108. 		{
  109. 			#region Help Text
  110.  
  111. 			/*
  112. 			DelTrash.exe,  Version 1.00
  113. 			Empty the Recycle Bin
  114.  
  115. 			Usage:   DelTrash.exe  [ drive: | /W ]  [ /C ]  [ /P ]
  116.  
  117. 			Where:   drive:  empty recycle bin on this drive only (default: all drives)
  118. 			         /W      empty recycle bin on Windows' drive only (default: all drives)
  119. 			         /C      prompt for Confirmation
  120. 			         /P      show Progress
  121.  
  122. 			Note:    Return code is 0 if the Recycle Bin was emptied successfully, or 1
  123. 			         if there was nothing to delete or in case of (command line) errors.
  124.  
  125. 			Credits: Based on code by Vinoth Kumar
  126. 			         www.codeproject.com/Articles/20172/Empty-the-Recycle-Bin-using-C
  127.  
  128. 			Written by Rob van der Woude
  129. 			http://www.robvanderwoude.com
  130. 			*/
  131.  
  132.  
  133. 			if ( errmsg.Length > 0 )
  134. 			{
  135. 				List<string> errargs = new List<string>( errmsg );
  136. 				errargs.RemoveAt( 0 );
  137. 				Console.Error.WriteLine( );
  138. 				Console.ForegroundColor = ConsoleColor.Red;
  139. 				Console.Error.Write( "ERROR:\t" );
  140. 				Console.ForegroundColor = ConsoleColor.White;
  141. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  142. 				Console.ResetColor( );
  143. 			}
  144.  
  145. 			Console.Error.WriteLine( );
  146.  
  147. 			Console.Error.WriteLine( "DelTrash.exe,  Version {0}", progver );
  148.  
  149. 			Console.Error.WriteLine( "Empty the Recycle Bin" );
  150.  
  151. 			Console.Error.WriteLine( );
  152.  
  153. 			Console.Error.Write( "Usage:   " );
  154. 			Console.ForegroundColor = ConsoleColor.White;
  155. 			Console.Error.WriteLine( "DelTrash.exe  [ drive: | /W ]  [ /C ]  [ /P ]" );
  156. 			Console.ResetColor( );
  157.  
  158. 			Console.Error.WriteLine( );
  159.  
  160. 			Console.Error.Write( "Where:   " );
  161. 			Console.ForegroundColor = ConsoleColor.White;
  162. 			Console.Error.Write( "drive:" );
  163. 			Console.ResetColor( );
  164. 			Console.Error.WriteLine( "  empty recycle bin on this drive only (default: all drives)" );
  165.  
  166. 			Console.ForegroundColor = ConsoleColor.White;
  167. 			Console.Error.Write( "         /W" );
  168. 			Console.ResetColor( );
  169. 			Console.Error.Write( "      empty recycle bin on " );
  170. 			Console.ForegroundColor = ConsoleColor.White;
  171. 			Console.Error.Write( "W" );
  172. 			Console.ResetColor( );
  173. 			Console.Error.WriteLine( "indows' drive only (default: all drives)" );
  174.  
  175. 			Console.ForegroundColor = ConsoleColor.White;
  176. 			Console.Error.Write( "         /C" );
  177. 			Console.ResetColor( );
  178. 			Console.Error.Write( "      prompt for " );
  179. 			Console.ForegroundColor = ConsoleColor.White;
  180. 			Console.Error.Write( "C" );
  181. 			Console.ResetColor( );
  182. 			Console.Error.WriteLine( "onfirmation" );
  183.  
  184. 			Console.ForegroundColor = ConsoleColor.White;
  185. 			Console.Error.Write( "         /P" );
  186. 			Console.ResetColor( );
  187. 			Console.Error.Write( "      show " );
  188. 			Console.ForegroundColor = ConsoleColor.White;
  189. 			Console.Error.Write( "P" );
  190. 			Console.ResetColor( );
  191. 			Console.Error.WriteLine( "rogress" );
  192.  
  193. 			Console.Error.WriteLine( );
  194.  
  195. 			Console.Error.WriteLine( "Note:    Return code is 0 if the Recycle Bin was emptied successfully, or 1" );
  196.  
  197. 			Console.Error.WriteLine( "         if there was nothing to delete or in case of (command line) errors." );
  198.  
  199. 			Console.Error.WriteLine( );
  200.  
  201. 			Console.Error.WriteLine( "Credits: Based on code by Vinoth Kumar" );
  202.  
  203. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  204. 			Console.Error.WriteLine( "         www.codeproject.com/Articles/20172/Empty-the-Recycle-Bin-using-C" );
  205. 			Console.ResetColor( );
  206.  
  207. 			Console.Error.WriteLine( );
  208.  
  209. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  210.  
  211. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  212.  
  213. 			#endregion Help Text
  214.  
  215. 			return 1;
  216. 		}
  217.  
  218.  
  219. 		public enum RecycleFlags : uint
  220. 		{
  221. 			RecycleNoConfirmation = 0x00000001,
  222. 			RecycleNoProgressUI = 0x00000002,
  223. 			RecycleNoSound = 0x00000004
  224. 		}
  225.  
  226.  
  227. 		[DllImport( "Shell32.dll", CharSet = CharSet.Unicode )]
  228. 		static extern uint SHEmptyRecycleBin( IntPtr hwnd, string pszRootPath, RecycleFlags dwFlags );
  229. 	}
  230. }
  231.  

page last uploaded: 2016-12-15, 11:20