using System;
using System.Reflection;
using System.IO;
using System.Diagnostics;

class App
{
    static int Main(string[] args)
    {
        // Find out what filename we have been given - we will want
        // to be launching a file with the same name, but an EXE
        // extension.

        string comName = Process.GetCurrentProcess().MainModule.FileName;


        // If we have an EXE extension, then this isn't going to work!

        if (Path.GetExtension(comName).ToLower() == ".exe")
        {
            Console.Error.WriteLine("This file must have a .COM extension");
            return 1;
        }


        // Make sure the target exists.

        string exeName = Path.ChangeExtension(comName, ".exe");
        if (!File.Exists(exeName))
        {
            Console.Error.WriteLine("Target executable '{0}' does not exist", exeName);
            return 1;
        }

        if (args.Length == 0)
        {
            // No params - spawn the real process and exit. This
            // will look to the user just like a normal windows
            // application does on startup when launched from
            // the command line.

            Process.Start(exeName);
            return 0;
        }
        else 
        {
            // There are parameters, so we want to behave like a
            // normal console app. Rather than launching the target
            // EXE, in a seperate process, we just run it in this one.
            // That way it will get to use our console.

            return AppDomain.CurrentDomain.ExecuteAssembly(exeName, null, args);
           
        }
    }
}

Copyright © 2002-2024, Interact Software Ltd. Please direct all Web site inquiries to webmaster@interact-sw.co.uk