RecipeDan
asked on
Pass arguments from c# to a Console Application
I am trying to pass arguments to a Console Application from C#. The console application works when I pass the argument from the command line like this:
The code that I am using in c# that does not call the Console Application is:
EnterInfo.exe 37 img05.png
//37 is the Record ID and img05.png is the image that I am moving to another folder and entering the information in a database
The code that I am using in c# that does not call the Console Application is:
//Call Console Application
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\inetpub\wwwroot\SportsData\EnterInfo.exe";
startInfo.Arguments = FileID + fileName;
Process.Start(startInfo);
The arguments do have values because I am able to display them on the page
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have no idea with respect to the web portion aspects of the question, but sometimes a working directory must also be set for a process to work correctly:
startInfo.FileName = @"C:\inetpub\wwwroot\SportsData\EnterInfo.exe";
startInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(startInfo.FileName);
ASKER
I created a script that gives me an error message when running the console application
I get this error:
Unhandled Exception: System.IndexOutOfRangeExce ption: Index was outside the bounds of the array. at EnterInfo.Program.Main(Str ing[] args)
So I am assuming that the arguments are not passing correctly to the Console Application. It gives line number 26 as the error and line 26 is this in Program.cs:
string ID = args[0]; //Line 26
string fileRequestName = args[1]; //Line 27
I get this error:
Unhandled Exception: System.IndexOutOfRangeExce
So I am assuming that the arguments are not passing correctly to the Console Application. It gives line number 26 as the error and line 26 is this in Program.cs:
string ID = args[0]; //Line 26
string fileRequestName = args[1]; //Line 27
string FileID = "37";
string fileName = "img05.png;
//Call Console Application
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\inetpub\wwwroot\SportsData\EnterInfo.exe";
startInfo.Arguments = FileID + " " + fileName;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
using (var process = Process.Start(startInfo))
{
string message;
using (var reader = process.StandardOutput)
{
message = reader.ReadToEnd();
label1.Text = message;
}
if (!string.IsNullOrEmpty(message))
{
label1.Text = message;
}
else
{
using (var reader = process.StandardError)
{
label1.Text = reader.ReadToEnd();
}
}
}
ASKER
I think I may know what the problem is when I run the console application when the web page says open, it works fine. However when I close the web page after I start the application it dos not work. So is there a way I can start the application and then close the web page?
ASKER