public static void ShutDownApp(object sender, EventArgs e)
        {
            Debug.WriteLine("Ending application...");

            // This imports any residual data files.  These files would exist if:
            //  - New data files were imported for which the user was editing the master copy at the time
            //  - AND the user chose not to import these new data files during the session.
            FormMain.ImportNewDataFiles();

            // Note: This new boolean setting was introduced in Jan 2007.
            if (SysInfo.Data.Options.DataXfer.UnattendedSync)
            {
                // Note: The introduction of additional parameter handling code means that all startup switches,
                //       like "DataTransfer", must now be prefixed with a "/" or a "\" or a "-".
        #if (DEBUG)
                //Note: This next if-else clause had to be introduced because early deployments of the app were done with Debug compilations
                if (Application.StartupPath.LastIndexOf("bin\\Debug") != -1)
                {
                    RegistryTools.SetAutoStartOnConnect(SysInfo.Data.Paths.App + "bin\\Debug\\" + SysInfo.Data.Admin.AppFilename, "/DataTransfer");
                }
                else
                {
                    RegistryTools.SetAutoStartOnConnect(SysInfo.Data.Paths.App + SysInfo.Data.Admin.AppFilename, "/DataTransfer");
                }
        #else
                RegistryTools.SetAutoStartOnConnect(SysInfo.Data.Paths.App + SysInfo.Data.Admin.AppFilename, "/DataTransfer");
        #endif
            }

            // Testing has revealed that we can leave 'GuestOnly' set to 1 all the time since ActiveSync
            // seems able to distinguish the primary Pocket PC from other ones.
            // 2006-06-18 - RW - I ran into a situation where this prevented me from establishing a partnership, so we're now going to reset it as originally planned!
            RegistryTools.SetGuestOnly(false);

            // Save the SysInfo object to disk
            string sysInfoFile = SysInfo.Data.Paths.App + "SysInfo.xml";
            Tools.SaveData(sysInfoFile, SysInfo.Data, null, DataObjects.Constants.ExportFormat.XML);
            File.SetAttributes(sysInfoFile, FileAttributes.Hidden);    // This is separate because it doesn't work with the CF

            Tools.HandleTempDirectory(SysInfo.Data.Paths.Temp, false); // Tries to erase Temp folder and all files within
            Application.ExitThread();
        }
        static void Main(string[] args)
        {
            Cursor.Current = Cursors.WaitCursor;
            //Application.EnableVisualStyles();  // Debug: Need to investigate this more before using

            // Try to load the assorted SysInfo in from disk.  If there's no file (first time ever or it was deleted) then initialize with default info
            InitializeSysInfo();           // Now we can set the rest of the reference data variables

            // Initialize Reference data such as geographical info, mobile devices, and languages
            //InitializeRefData();  // Debug: To Do!

            Tools.HandleTempDirectory(SysInfo.Data.Paths.Temp, true); // Ensures Temp folder exists and all possible files are removed

            // Check if any command line arguments were passed to the executable.  Among other things...
            // Find out if the "DataTransfer" parameter was passed to the EXE upon startup.
            // If so, then 'SysInfo.Data.Admin.DataTransferActive' will be set true.
            HandleStartupParameters(args);

            if (Uninstall)
            {
                return; // Quietly exit
            }
            else if (!SysInfo.Data.Admin.DataTransferActive)
            {
                if (SysInfo.Data.Options.ShowSplash)
                {
                    frmSplash.ShowSplashScreen();
                    Application.DoEvents();
                }
            }

            // Now see whether a copy of the app is already running.
            Process aProcess  = Process.GetCurrentProcess();
            string  aProcName = aProcess.ProcessName;

            // Determine how we're going to proceed
            if (Process.GetProcessesByName(aProcName).Length == 1) // If true, then this is the first copy of the executable to be started
            {
                // If this executable is running then we DON'T want another copy started by ActiveSync
                RegistryTools.DeleteAutoStartOnConnect();

                // This should always be set to true so that the partnership dialog box doesn't appear
                RegistryTools.SetGuestOnly(true);

                Application.ApplicationExit += new EventHandler(ShutDownApp); // Method that'll be called when shutting down

                // Instantiate the poll manager
                _current = new PollManager();

                // Instantiate an instance of frmMain
                FormMain = new frmMain();
                FormMain.SetMenuItems(0);

                // This imports any residual data files that failed to get imported the last time the app was run.  Generally the only
                // time such files would exist would be if the app previously crashed.
                FormMain.ImportNewDataFiles();

                InitializeCopyData();

                TestCode(); // Only used during debugging; normally empty

                bool okayToStartupApp = true;

                // Check whether the app is being run for the very first time ever.
                if (SysInfo.Data.Options.PrimaryUser == "")
                {
                    FormMain.Show(); // Doing this simply to provide standard visual background (behind Options form)

                    // We'll startup the Options form in a special way that will force (strongly request) a UserName be specified
                    frmOptions optionsForm = new frmOptions(true);

                    // If user really, really didn't want to specify a username then we MUST exit app
                    if (SysInfo.Data.Options.PrimaryUser == "")
                    {
                        okayToStartupApp = false;
                        FormMain.ExitApp();
                    }
                }

                // Now check that the file association(s) are properly setup for this app
                CheckFileAssociations();

                if (okayToStartupApp)
                {
                    if (Tools.IsAppExpired())
                    {
                        FormMain.Freeze();
                    }
                    else
                    {
                        // "StartupFilename" is set in HandleStartupParameters
                        if (StartupFilename != null)
                        {
                            FormMain.PreparePoll(StartupFilename);
                        }
                    }

                    Cursor.Current = Cursors.Default;
                    Application.Run(FormMain);
                }
            }

            else // A copy of the app is already running
            {
                if (SysInfo.Data.Admin.DataTransferActive)
                {
                    // Since the data transfer thread will already have been started by the RAPI.ActiveSync.Active event
                    // we don't need to start a 2nd copy of this application.  So just exit quietly.
                    // Note: Because we now remove the AutoStartOnConnect string, we'll actually never get to here
                    //       but it doesn't hurt to keep this extra code regardless.
                    Application.ExitThread();
                }
                else
                {
                    // if another process exists, send file name as message and quit
                    if (StartupFilename != null)
                    {
                        // Create a new instance of the class
                        CopyData copyData = new CopyData();

                        // Create the named channels to send and receive on.  The name is arbitrary; it can be anything you want.
                        copyData.Channels.Add(SysInfo.Data.Admin.AppName);

                        // Send filename
                        copyData.Channels[SysInfo.Data.Admin.AppName].Send(StartupFilename);
                    }
                    else // 2nd copy started without any specific file so don't allow it
                    {
                        // Prevent 2nd copy of executable from running
                        Tools.ShowMessage("The " + SysInfo.Data.Admin.AppName + " Desktop application is already running!", SysInfo.Data.Admin.AppName);
                        Application.ExitThread();
                    }
                }
            }
        } // End of "Main"