Not able to launch application on second time without arguments if arguments passed is wrong

  • Thread starter Thread starter Joy123456
  • Start date Start date
J

Joy123456

Guest
I have created a WinFrom application WindowsFormsApp2 as follows

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string [] args)
{
for (int i = 0; i < args.Length; i++)
{
if (args == "Launch")
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else if(args == "Exception")
{
throw new Exception("Launch failed");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
}
}


To launch this application from WindowsFormsApp3 app I have written the code as below

private void button1_Click(object sender, EventArgs e)
{
try
{
using (Process process = new Process())
{
process.StartInfo.FileName = @"C:\Users\MYNAME\source\repos\WindowsFormsApp3\WindowsFormsApp3\bin\Debug\WindowsFormsApp3.exe";
process.StartInfo.Arguments = @"Exception";
if (process.Start())
{
}
else
{
process.StartInfo.Arguments = string.Empty;
process.Start();
}
}
}
catch (Exception ex)
{
using (Process process = new Process())
{
process.StartInfo.FileName = @"C:\Users\MYNAME\source\repos\WindowsFormsApp3\WindowsFormsApp3\bin\Debug\WindowsFormsApp3.exe";
process.StartInfo.Arguments = @"Exception";
if (process.Start())
{

}
else
{
process.StartInfo.Arguments = string.Empty;
process.Start();
}
}

}
}

What I want is if the argument passes throws exception I need to relaunch the application without arguments.

But here always process.Start() returns true;

Thanks in advance!!..

Continue reading...
 
Back
Top