[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Pass arguments from c# to a Console Application

Posted on 2015-01-27
5
Medium Priority
?
343 Views
Last Modified: 2015-02-03
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:
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

Open in new window


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);

Open in new window

The arguments do have values because I am able to display them on the page
0
Comment
Question by:RecipeDan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 40573053
What response are you getting from your call to the console, if any?

Try passing your parameters with a space between them.
i.e.
startInfo.Arguments = FileID + " " + fileName;

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40573113
I am not getting a response from the call to the console. I tried your suggestion and the executable does not run. This is the first time that I am calling a console application from a web page. Is there an error log that I can create? How do I debug something like this?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40573279
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);

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40573349
I created a script that gives me an error message when running the console application

I get this error:

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at EnterInfo.Program.Main(String[] 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

            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();
                            }
                        }
                    }

Open in new window

0
 
LVL 1

Author Comment

by:RecipeDan
ID: 40573535
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?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question