Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for wpd.cs

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.IO;
  6. using System.Windows.Forms;
  7.  
  8. namespace RobvanderWoude
  9. {
  10.     static class WPD
  11.     {
  12.         /// <summary>
  13.         /// The main entry point for the application.
  14.         /// </summary>
  15.         [STAThread]
  16.         static void Main()
  17.         {
  18.             int exitcode = 1;
  19.             Application.EnableVisualStyles( );
  20.             Application.SetCompatibleTextRenderingDefault( false );
  21.             string[] arguments = System.Environment.GetCommandLineArgs( ).Skip( 1 ).ToArray( );
  22.             if ( arguments.Length == 1 )
  23.             {
  24.                 if ( arguments[0] == "/?" )
  25.                 {
  26.                     string message = "WPD.exe, Version 1.00\n";
  27.                     message += "Open a Word document with identical name and location\n";
  28.                     message += "when doubeclicking a WordPerfect document\n\n";
  29.                     message += "Usage:\n";
  30.                     message += "  *\tRegister this program for WordPerfect files (*.wpd)\n";
  31.                     message += "  *\tWhenever you doubleclick a WordPerfect file, this\n";
  32.                     message += "   \tprogram will search for a Word document with the\n";
  33.                     message += "   \tsame name and location, and open that Word\n";
  34.                     message += "   \tdocument instead.\n";
  35.                     message += "  *\tIf more than one matching Word file exists\n";
  36.                     message += "   \t(e.g. *.doc as well as *.docx), a popup message\n";
  37.                     message += "   \twill tell you to select the Word document directly.\n\n";
  38.                     message += "Return code for this program is 0 if a single matching Word\n";
  39.                     message += "document was found and opened, otherwise 1.\n\n";
  40.                     message += "Written by Rob van der Woude\n";
  41.                     message += "https://www.robvanderwoude.com";
  42.                     string title = "Help for WPD.exe";
  43.                     MessageBox.Show( message, title );
  44.                 }
  45.                 else
  46.                 {
  47.                     string ext = Path.GetExtension( arguments[0].ToLower( ) );
  48.                     string workingdir = Directory.GetParent( arguments[0] ).FullName;
  49.                     string basename = Path.GetFileNameWithoutExtension( arguments[0] );
  50.                     if ( ext.Substring( 0, 3 ) == ".wp" )
  51.                     {
  52.                         List<string> docfiles = Directory.GetFiles( workingdir, basename + ".doc*" ).ToList<string>( );
  53.                         switch ( docfiles.Count )
  54.                         {
  55.                             case 1:
  56.                                 try
  57.                                 {
  58.                                     _ = Process.Start( new ProcessStartInfo( docfiles[0] ) );
  59.                                     exitcode = 0;
  60.                                 }
  61.                                 catch
  62.                                 {
  63.                                     // ignore, exit code will be 1
  64.                                 }
  65.                                 break;
  66.                             default:
  67.                                 MessageBox.Show( string.Format( "There are {0} files matching \"{1}.doc*\"", docfiles.Count, Path.Combine( workingdir, basename ) ), "Ambiguity" );
  68.                                 break;
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.             Environment.ExitCode = exitcode;
  74.         }
  75.     }
  76. }
  77.  

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