Hi there again, looks like I ran into trouble again. Well, programming doesn't look simple to me.

What I want to know is how do I invoke an executable file from a c# application? Are the procedures same in both console applications and windows application?

Recommended Answers

All 5 Replies

>how do I invoke an executable file from a c# application
To run exe file - use the methods of System.Diagnostics.Process class.

A good book to reference on this issue is windows forms programming 2.0 best $60 i ever spent! It has what you need in it.

Cheers'
Jamesonh20

Here you can invoke an exe on another thread so while you're waiting for the program to load your application will continue running. This will wait until the process has started then move the window:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace daniweb
{
  public partial class frmMoveWindow : Form
  {
    public frmMoveWindow()
    {
      InitializeComponent();
    }

    [DllImport("user32.dll")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll")]
    public static extern int GetTopWindow();
    [DllImport("user32.dll")]
    public static extern bool UpdateWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    public static extern int GetForegroundWindow();
    [DllImport("user32.dll")]
    public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
    [DllImport("User32.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int cx, int cy, bool repaint);
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsWindowVisible(IntPtr hWnd);

    public const int WM_COMMAND = 0x0112;
    public const int WM_CLOSE = 0xF060;

    private void button1_Click(object sender, EventArgs e)
    {
      const string fName = @"C:\dotnetwin\daniweb.vb\bin\Debug\daniweb.vb.exe";
      new Action<string>(Start).BeginInvoke(fName, new AsyncCallback(StartCallback), null);
    }

    private static void Start(string fName)
    {
      const int MAX_WAIT = 200;
      int counter = 0;
      using (Process p = Process.Start(fName))
      {
        while ((p.MainWindowHandle == IntPtr.Zero) || !IsWindowVisible(p.MainWindowHandle))
        {
          System.Threading.Thread.Sleep(10);
          p.Refresh();
          counter++;
          if (counter > MAX_WAIT)
            return;
        }
        p.WaitForInputIdle(10000);
        MoveWindow(p.MainWindowHandle, 0, 0, 400, 400, true);
      }
    }
    private static void StartCallback(IAsyncResult ar)
    {
      System.Runtime.Remoting.Messaging.AsyncResult result = (System.Runtime.Remoting.Messaging.AsyncResult)ar;
      Action<string> del = (Action<string>)result.AsyncDelegate;
      try
      {
        del.EndInvoke(ar);
      }
      catch { }
    }

  }
}

So there you have an extreme example of starting a process, or more simply:

private void button3_Click(object sender, EventArgs e)
    {
      System.Diagnostics.Process.Start(@"C:\data\Spreadsheet.xls").Dispose();
    }

first u add this class:

using System.Diagnostics;

then add this code:in d button_click or somewhere else.

p = new Process();
p.StartInfo.FileName = "C:\\path";
p.Start();

Thanks a lot all of you guys, your suggestions, advice and examples have been very helpful.

Thanks to all of you, my app is running just the way I wanted it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.