using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace PowerDVDLauncher
{
    static class Program
    {
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
        private const int SW_SHOWMAXIMIZED = 3;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            // Attempt to bring an existing PowerDVD to the foreground.
            // If none exists, open PowerDVD.
            if(!BringProcessToForeground())
                OpenPowerDVD();               
        }
        private static bool BringProcessToForeground()
        {
            Process[] processes = Process.GetProcessesByName([B][COLOR="#FF0000"]"PowerDVD"[/COLOR][/B]);
            if (processes.Length != 0)
            {
                // If PowerDVD is currently running, bring it to the foreground
                IntPtr hWnd = processes[0].MainWindowHandle;
                ShowWindow(hWnd, SW_SHOWMAXIMIZED);
                SetForegroundWindow(hWnd);
                return true;
            }
            return false;
        }
        private static void OpenPowerDVD()
        {
                string x64Path = @"[B][COLOR="#FF0000"]C:\Program Files\PowerDVD\PowerDVD.exe[/COLOR][/B]";
                LaunchPowerDVDProcess(x64Path);
   
        }
        private static bool LaunchPowerDVDProcess(string path)
        {
            if (path != null && File.Exists(path))
            {
                string args = "";
                Process proc = new Process();
                if(File.Exists("PowerDVDLaunchArgs.txt"))
                {
                    using (StreamReader argStream = File.OpenText("PowerDVDLaunchArgs.txt"))
                    {
                        args = argStream.ReadLine();
                        argStream.Close();
                    }
                }
                       
                proc.StartInfo = new ProcessStartInfo(path, args);
                proc.Start();
                BringProcessToForeground();
                return true;
            }
            return false;
        }
    }
}