Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to execute a file from a C# program

nikhil500nikhil500 Posts: 3Member
I am writing a C# program and need to run a .exe file from within it. (similar to calling system() function in C).

Can somebody tell me how to go about it?

Thanks

Comments

  • IDKIDK Posts: 1,784Member
    : I am writing a C# program and need to run a .exe file from within it. (similar to calling system() function in C).
    :
    : Can somebody tell me how to go about it?
    :
    : Thanks
    :
    Use the funktion Start()
    in the Process class
    in the namespace
    System.Diagnostics

    Or the compact one:
    System.Diagnostics.Process.Start()
    The PROgrammer Niklas Ulvinge

  • Yepper65Yepper65 Posts: 29Member
    Hi,

    I just posted the answer on this question a few pages ago when i saw your question.
    If you want to start a program from your application,you should go to your toolbox and select the components Tab and drag a Process to your application window.
    Under the properties of the created process1, you can set under StartInfo the FileName of the programm to be opened, in this sample Messenger.
    After this you create a button and create an Event, let's say Start Messenger.
    Hit the Enter tab and type process1.open();
    After debugging your application should open your Messenger Login screen.

    With friendly greetings,


    Yepper65
    =======================================================================
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace MessengerStart
    {
    ///
    /// Summary description for Form1.
    ///
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Diagnostics.Process process1;
    ///
    /// Required designer variable.
    ///
    private System.ComponentModel.Container components = null;

    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }

    ///
    /// Clean up any resources being used.
    ///
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.process1 = new System.Diagnostics.Process();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(72, 32);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "Start";
    this.button1.Click += new System.EventHandler(this.StartMessenger_Click);
    //
    // process1
    //
    this.process1.StartInfo.FileName = "C:\Program Files\MSN Messenger\msnmsgr.exe";
    this.process1.SynchronizingObject = this;
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(224, 86);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Start Messenger";
    this.ResumeLayout(false);

    }
    #endregion

    ///
    /// The main entry point for the application.
    ///
    [STAThread]
    static void Main()
    {
    Application.Run(new Form1());
    }

    private void StartMessenger_Click(object sender, System.EventArgs e)
    {
    process1.Start();
    }
    }
    }




  • non-monotonicnon-monotonic Posts: 17Member
    The last method posted by yepper is overkill you should just use System.Diagnostics.Process.Start("app.exe") much easier.
    A slave to the code

  • Yepper65Yepper65 Posts: 29Member
    : The last method posted by yepper is overkill you should just use System.Diagnostics.Process.Start("app.exe") much easier.
    : A slave to the code
    :
    :
    Hi mono-tonic,

    This is not inteded to be critic on what you wrote but an addition to it because i found out recently that not all application execute when you use System.Diagnostics.Process.Start("app.exe"), for example i have a program called GanttProject(http://ganttproject.sourceforge.net) in my program folder and when i don't specify the path i get this error:
    -----------------------------------------------------------------------
    An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in system.dll

    Additional information: The system cannot find the file specified
    -----------------------------------------------------------------------
    So it seems that it's more sure when you give the full path to the application because when i write: System.Diagnostics.Process.Start("C:\Program Files\GanttProject\ganttproject.exe"); , the program starts without problems.
    But you are right that using your way is a shorter way to start programs such as Wordpad and Messenger,Outlook etc.

    With friendly greetings,


    Yepper65
  • IDKIDK Posts: 1,784Member
    Thats becouse you're isn't in the map you want to be.
    But if you just want to start an application you don't
    need to have all that code.
    Only if you want to do something with it you'll need
    to create an object of Process.
    ===============================
    El [b]PRO[/b]grammador Niklas Ulvinge

Sign In or Register to comment.