Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listverbs.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using Microsoft.Win32;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	internal class ListVerbs
  11. 	{
  12. 		static readonly string progver = "1.01";
  13.  
  14. 		static int rc = 0;
  15.  
  16.  
  17. 		static int Main( string[] args )
  18. 		{
  19. 			rc = 0;
  20. 			bool verbose = false;
  21.  
  22. 			if ( args.Length == 0 || args.Length > 2 || args.Contains( "/?" ) )
  23. 			{
  24. 				return ShowHelp( );
  25. 			}
  26. 			string ext = args[0];
  27. 			if ( args.Length == 2 )
  28. 			{
  29. 				if ( args[1].ToUpper( ).StartsWith( "/V" ) )
  30. 				{
  31. 					verbose = true;
  32. 				}
  33. 				else
  34. 				{
  35. 					return ShowHelp( "Invalid command line argument \"{0}\"", args[1] );
  36. 				}
  37. 			}
  38. 			if ( ext[0] != '.' )
  39. 			{
  40. 				verbose = true;
  41. 			}
  42.  
  43. 			if ( verbose )
  44. 			{
  45. 				GetVerbsVerbose( args[0] );
  46. 			}
  47. 			else
  48. 			{
  49. 				GetVerbs( args[0] );
  50. 			}
  51.  
  52. 			return rc;
  53. 		}
  54.  
  55.  
  56. 		static void GetVerbs( string ext )
  57. 		{
  58. 			ProcessStartInfo startinfo = new ProcessStartInfo( string.Format( "fakefilename{0}", ext ) );
  59. 			foreach ( string verb in startinfo.Verbs )
  60. 			{
  61. 				if ( rc == 0 )
  62. 				{
  63. 					Console.WriteLine( "Verbs for {0}", ext );
  64. 					Console.WriteLine( "=========={0}", new String( '=', ext.Length ) );
  65. 				}
  66. 				rc++;
  67. 				Console.WriteLine( verb );
  68. 			}
  69. 		}
  70.  
  71.  
  72. 		static void GetVerbsVerbose( string ext )
  73. 		{
  74. 			string doctype = string.Empty;
  75. 			string docname = string.Empty;
  76. 			string regkeypath = string.Format( "Software\\Classes\\{0}", ext );
  77. 			if ( ext[0] == '.' )
  78. 			{
  79. 				// get document type for specified file extension
  80. 				Console.WriteLine( "Extension = {0}", ext );
  81. 				using ( RegistryKey regkey = Registry.LocalMachine.OpenSubKey( regkeypath ) )
  82. 				{
  83. 					if ( regkey != null )
  84. 					{
  85. 						object doctypevalue = regkey.GetValue( "" );
  86. 						if ( doctypevalue != null )
  87. 						{
  88. 							doctype = doctypevalue.ToString( );
  89. 						}
  90. 					}
  91. 				}
  92. 			}
  93. 			else
  94. 			{
  95. 				// assume document type is specified on the command line
  96. 				doctype = ext;
  97. 			}
  98. 			if ( !string.IsNullOrWhiteSpace( doctype ) )
  99. 			{
  100. 				regkeypath = string.Format( "Software\\Classes\\{0}\\CurVer", doctype );
  101. 				using ( RegistryKey regkey = Registry.LocalMachine.OpenSubKey( regkeypath ) )
  102. 				{
  103. 					if ( regkey != null )
  104. 					{
  105. 						object doctypevalue = regkey.GetValue( "" );
  106. 						if ( doctypevalue != null )
  107. 						{
  108. 							doctype = doctypevalue.ToString( );
  109. 						}
  110. 					}
  111. 				}
  112. 			}
  113. 			if ( !string.IsNullOrWhiteSpace( doctype ) )
  114. 			{
  115. 				// get proper casing of docyument type, as used in the registry
  116. 				string[] classes = Registry.LocalMachine.OpenSubKey( "Software\\Classes" ).GetSubKeyNames( );
  117. 				foreach ( string classname in classes )
  118. 				{
  119. 					if ( classname.ToLower( ) == doctype.ToLower( ) )
  120. 					{
  121. 						doctype = classname;
  122. 					}
  123. 				}
  124. 				Registry.LocalMachine.Close( );
  125. 				// get friendly name for document type
  126. 				regkeypath = string.Format( "Software\\Classes\\{0}", doctype );
  127. 				using ( RegistryKey regkey = Registry.LocalMachine.OpenSubKey( regkeypath ) )
  128. 				{
  129. 					if ( regkey != null )
  130. 					{
  131. 						docname = regkey.GetValue( "" ).ToString( );
  132. 					}
  133. 				}
  134. 			}
  135. 			if ( !string.IsNullOrWhiteSpace( doctype ) )
  136. 			{
  137. 				Console.Write( "File Type = {0}", doctype );
  138. 				if ( docname != null )
  139. 				{
  140. 					Console.Write( " ({0})", docname );
  141. 				}
  142. 				Console.WriteLine( );
  143. 				// get all available verbs for this file type
  144. 				regkeypath = string.Format( "Software\\Classes\\{0}\\shell", doctype );
  145. 				using ( RegistryKey regkey = Registry.LocalMachine.OpenSubKey( regkeypath ) )
  146. 				{
  147. 					if ( regkey != null )
  148. 					{
  149. 						int columnwidth = 0;
  150. 						Console.WriteLine( "Verbs:" );
  151. 						// all verbs have a separate subkey
  152. 						foreach ( string verb in regkey.GetSubKeyNames( ) )
  153. 						{
  154. 							columnwidth = Math.Max( columnwidth, verb.Length );
  155. 						}
  156. 						foreach ( string verb in regkey.GetSubKeyNames( ) )
  157. 						{
  158. 							columnwidth = Math.Max( columnwidth, verb.Length );
  159. 							rc++;
  160. 							// get the command for this verb
  161. 							using ( RegistryKey regkeycommand = regkey.OpenSubKey( string.Format( "{0}\\command", verb ) ) )
  162. 							{
  163. 								if ( regkeycommand != null )
  164. 								{
  165. 									string command = regkeycommand.GetValue( "" ).ToString( );
  166. 									Console.WriteLine( "\t{0,-" + columnwidth + "}        {1}", verb.ToLower( ), command );
  167. 								}
  168. 							}
  169. 						}
  170. 					}
  171. 				}
  172. 			}
  173. 		}
  174.  
  175.  
  176. 		static int ShowHelp( params string[] errmsg )
  177. 		{
  178. 			#region Help Text
  179.  
  180. 			/*
  181. 			ListVerbs.exe,  Version 1.00
  182. 			List all verbs for the specified file extension
  183.  
  184. 			Usage:   ListVerbs   filetype  [ /V ]
  185.  
  186. 			Where:   filetype    is the file type to list the available verbs for
  187. 			         /V          Verbose output, i.e. not just the verbs, but the
  188. 			                     associated commands as well
  189.  
  190. 			Notes:   filetype can be specified as file extension including dot,
  191. 			         e.g. ".odt", or as document type name associated with a file
  192. 			         extension, e.g. "LibreOffice.WriterDocument"; in the latter
  193. 			         case, output will be verbose whether /V switch is used or not.
  194. 			         Verbose mode may sometimes find more verbs than normal mode.
  195. 			         Return code ("errorlevel") equals the number of verbs found,
  196. 			         or -1 in case of (command line) errors.
  197.  
  198. 			Written by Rob van der Woude
  199. 			https://www.robvanderwoude.com
  200. 			*/
  201.  
  202. 			#endregion Help Text
  203.  
  204.  
  205. 			#region Error Message
  206.  
  207. 			if ( errmsg.Length > 0 )
  208. 			{
  209. 				List<string> errargs = new List<string>( errmsg );
  210. 				errargs.RemoveAt( 0 );
  211. 				Console.Error.WriteLine( );
  212. 				Console.ForegroundColor = ConsoleColor.Red;
  213. 				Console.Error.Write( "ERROR:\t" );
  214. 				Console.ForegroundColor = ConsoleColor.White;
  215. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  216. 				Console.ResetColor( );
  217. 			}
  218.  
  219. 			#endregion Error Message
  220.  
  221.  
  222. 			#region Display Help Text
  223.  
  224. 			Console.Error.WriteLine( );
  225.  
  226. 			Console.Error.WriteLine( "ListVerbs.exe,  Version {0}", progver );
  227.  
  228. 			Console.Error.WriteLine( "List all verbs for the specified file extension" );
  229.  
  230. 			Console.Error.WriteLine( );
  231.  
  232. 			Console.Error.Write( "Usage:   " );
  233. 			Console.ForegroundColor = ConsoleColor.White;
  234. 			Console.Error.WriteLine( "ListVerbs   filetype  [ /V ]" );
  235. 			Console.ResetColor( );
  236.  
  237. 			Console.Error.WriteLine( );
  238.  
  239. 			Console.Error.Write( "Where:   " );
  240. 			Console.ForegroundColor = ConsoleColor.White;
  241. 			Console.Error.Write( "filetype" );
  242. 			Console.ResetColor( );
  243. 			Console.Error.WriteLine( "    is the file type to list the available verbs for" );
  244.  
  245. 			Console.ForegroundColor = ConsoleColor.White;
  246. 			Console.Error.Write( "         /V          V" );
  247. 			Console.ResetColor( );
  248. 			Console.Error.WriteLine( "erbose output, i.e. not just the verbs, but the" );
  249.  
  250. 			Console.Error.WriteLine( "                     associated commands as well" );
  251.  
  252. 			Console.Error.WriteLine( );
  253.  
  254. 			Console.Error.Write( "Notes:   " );
  255. 			Console.ForegroundColor = ConsoleColor.White;
  256. 			Console.Error.Write( "filetype" );
  257. 			Console.ResetColor( );
  258. 			Console.Error.WriteLine( " can be specified as file extension including dot," );
  259.  
  260. 			Console.Error.WriteLine( "         e.g. \".odt\", or as document type name associated with a file" );
  261.  
  262. 			Console.Error.WriteLine( "         extension, e.g. \"LibreOffice.WriterDocument\"; in the latter" );
  263.  
  264. 			Console.Error.Write( "         case, output will be verbose whether " );
  265. 			Console.ForegroundColor = ConsoleColor.White;
  266. 			Console.Error.Write( "/V" );
  267. 			Console.ResetColor( );
  268. 			Console.Error.WriteLine( " switch is used or not." );
  269.  
  270. 			Console.Error.WriteLine( "         Verbose mode may sometimes find more verbs than normal mode." );
  271.  
  272. 			Console.Error.WriteLine( "         Return code (\"errorlevel\") equals the number of verbs found," );
  273.  
  274. 			Console.Error.WriteLine( "         or -1 in case of (command line) errors." );
  275.  
  276. 			Console.Error.WriteLine( );
  277.  
  278. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  279.  
  280. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  281.  
  282. 			#endregion Display Help Text
  283.  
  284.  
  285. 			return -1;
  286. 		}
  287. 	}
  288. }
  289.  

page last modified: 2024-04-16; loaded in 0.0067 seconds