Example #1
0
        //
        //  static private void EnableAutoOpenProfile( object, EventArgs )
        //
        //  One of the features of GAVPI is that of associating a Profile with an executable file (whether
        //  an application, game, etc) and having that Profile automatically loaded whenever that executable
        //  is ran.  To facilitate this, a node is allocated within a Profile that holds the fully qualified
        //  filename of an associated executable; these associations are collated at initial GAVPI startup,
        //  and the assumption is made that all Profiles are stored within the Profiles sub-folder in which
        //  GAVPI resides.  We then request the Operating System inform GAVPI when a process starts, and via
        //  a trivial process monitor with callbacks.
        //
        public static void EnableAutoOpenProfile( object sender, EventArgs e )
        {
            //  Get the path to the running instance of GAVPI, and from there expand it with the Profiles
            //  sub-folder.

            string ProfilePath = new Uri( System.IO.Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase ) ).LocalPath + "\\Profiles";

            //  If there isn't a Profiles directory within the directory GAVPI was launched from, go no further.

            if( !Directory.Exists( ProfilePath ) ) return;

            XmlDocument Profile = new XmlDocument();

            //  Enumerate the XML Profiles within the sub-folder, extracting the filename of a user-chosen
            //  executable whose process startup we may monitor.  Exceptions are likely limited to badly formed
            //  XML - or some other kind of data but with an .XML extension.

            foreach( string ProfileFilename in Directory.EnumerateFiles( ProfilePath, "*.xml" ) ) {

                try {

                    Profile.Load( ProfileFilename );

                    XmlNode AssociatedProcess = Profile.SelectSingleNode( ASSOCIATED_PROCESS_XML_PATH );

                    //  We'll store the executable's filename within a key/value Dictionary for later
                    //  scruitiny (see OnProcessStarted(), later).

                    if( AssociatedProcess != null && AssociatedProcess.InnerText != null )
                        AddAutoOpenProfile( AssociatedProcess.InnerText, ProfileFilename );

                } catch( Exception ) { break; }

            }  //  foreach()

            //  We'll start the process monitor whether we have a associations to monitor since associations may
            //  be added at runtime via frmProfile.

            AssociatedProcessMonitor = new ProcessMonitor( OnProcessStarted, OnProcessStopped );

            AssociatedProcessMonitor.Start();
        }