2006-02-23

abrir un fichero con process start

Primero lo intentamos abrir con "Process.Start", y luego usamos el handle que devuelve para poner la ventana delante. El problema es que si la aplicación ya estaba abierta, no devuelve nada así que lo que hacemos es buscar una ventana en cuyo título aparezca el fichero que queremos abrir...


using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace cancelarConexiondeRed
{
        /// <summary>
        /// Descripción breve de Class1.
        /// </summary>
        class Class1
        {



                [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
                public static extern bool SetWindowPos(
                        IntPtr hWnd,               // window handle
                        IntPtr hWndInsertAfter,    // placement-order handle
                        int X,                  // horizontal position
                        int Y,                  // vertical position
                        int cx,                 // width
                        int cy,                 // height
                        uint uFlags);           // window positioning flags

                public const int HWND_TOP            = 0x0;
                public const int HWND_TOPMOST    = -1;
                public const int HWND_BOTTOM     = 0x1;
                public const uint SWP_NOSIZE     = 0x1;
                public const uint SWP_NOMOVE     = 0x2;
                public const uint SWP_SHOWWINDOW = 0x40;


               
                [DllImport("user32.dll")]
                static extern IntPtr SetActiveWindow(IntPtr hWnd);
                [DllImport("user32.dll", SetLastError=true)]
                static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

                [STAThread]
                static void Main(string[] args)
                {
                        Process ventana = Process.Start(@"u:\teclas.txt");
                        Console.ReadLine();
                        if (ventana!=null)
                                SwitchToThisWindow(ventana.MainWindowHandle, true);
                        else
                        {

                                Process[] myProcesses = Process.GetProcesses();
                                foreach(Process myProcess in myProcesses)
                                {
                                        if (myProcess.MainWindowTitle.IndexOf(@"u:\teclas.txt") != -1)
                                        {
                                                SwitchToThisWindow(myProcess.MainWindowHandle, true);
                                                break;
                                        }
                               
                                }
                        }
                }

        }


No hay comentarios: