How to use Arguments in ProcessStartInfo

MiPakTeh 1,476 Reputation points
2021-10-02T13:15:53.14+00:00

Hi All,

I want start the program created by C# using this code.How?
the name of program, copy from "C:\Users\family\source\repos\Third\Third\bin\Release\Third.exe".
What Actual code at "Start.Arguments = ?"

code is;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace Act_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Prepare the process to run
            ProcessStartInfo start = new ProcessStartInfo();
            // Enter in the command line arguments, everything you would enter after the executable name itself
            start.Arguments = arguments;
            // Enter the executable to run, including the complete path
            start.FileName = "C:\\Users\\family\\Desktop\\Third.exe";
            // Do you want to show a console window?
            start.WindowStyle = ProcessWindowStyle.Hidden;
            start.CreateNoWindow = true;
            int exitCode;


            // Run the external process & wait for it to finish
            using (Process proc = Process.Start(start))
            {
                proc.WaitForExit();

                // Retrieve the app's exit code
                exitCode = proc.ExitCode;
            }
        }
    }
}
Developer technologies C#
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-10-04T10:58:18.803+00:00

    See the following for an example that in this case uses PowerShell but can be any executable. No window is created, output is sent to a file (can be eliminated and still works), waits for the process to finish via await.

    For a more complex set of arguments using the same pattern see this method which is in the same class as the last example.

    Both examples were written with C#9.

    Image below is from the first link above

    137365-figure1.png

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.