• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1056
  • Last Modified:

C# running external command prompt programs on C# programs

Hi there! I am trying to allow my C# program to run an external perl script and output the results on a rich text box on a form. The problem arises with an error of:

Unhandled Exception: System.ComponentModel.Win32Exception: The system cannot fin
d the file specified
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startI
nfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(String fileName, String arguments)
   at ConsoleCommands.Program.Main(String[] args) in C:\Users\Aaron\Desktop\Sys
crawl\ConsoleCommands\ConsoleCommands\Program.cs:line 14

My codes as the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;


namespace ConsoleCommands
{
    class Program
    {
        static void Main(string[] args)
        {
            Process.Start("perl.exe", "C:\\syscrawl\\ftk\\bin\\mactime.pl -b testing.bodyfile -z UCT-8 > testing.txt");
        }
    }
}

Open in new window


When running the command purely on the CMD it works perfectly! Can someone please guide me to the problem? And can you please provide some clues for the output of the results to a rich text box for the C# form? Thanks!
0
VMthinker
Asked:
VMthinker
  • 3
  • 3
2 Solutions
 
arif_eqbalCommented:
looks like you need to specify the path to Perl.exe
i think when you say it runs fine from cmd prompt you would already be in the path where this exe lies...
0
 
Carl TawnSystems and Integration DeveloperCommented:
or is perl.exe is already in $PATH then you could try:
Process process = new Process();
process.StartInfo.Filename = "C:\\syscrawl\\ftk\\bin\\mactime.pl"
process.StartInfo.Arguments = "-b testing.bodyfile -z UCT-8 > testing.txt";
process.ShellExecute = false;
process.RedirectStandardOutput = true;
process.Start();

System.IO.StreamReader reader = process.StandardOutput;  
string sRes = reader.ReadToEnd();  
reader.Close(); 

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Actually, scratch that, you still need perl.exe if you want to catch the output:
Process process = new Process();
process.StartInfo.Filename = "C:\\path_to_perl\\perl.exe";
process.StartInfo.Arguments = "C:\\syscrawl\\ftk\\bin\\mactime.pl -b testing.bodyfile -z UCT-8 > testing.txt";
process.ShellExecute = false;
process.RedirectStandardOutput = true;
process.Start();

System.IO.StreamReader reader = process.StandardOutput;  
string sRes = reader.ReadToEnd();  
reader.Close();  

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
VMthinkerAuthor Commented:
Carl your codes were all wrong!

It should be something like this:

ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "C:\\path_to_perl\\perl.exe";
            startInfo.Arguments = "C:\\syscrawl\\ftk\\bin\\mactime.pl -b testing.bodyfile -z UCT-8 > testing.txt";
            startInfo.UseShellExecute = false;
            startInfo.RedirectStandardOutput = true;
            Process.Start(startInfo);

Open in new window


Please submit codes that VS 2010 can read. Thanks!
0
 
Carl TawnSystems and Integration DeveloperCommented:
The code was not ALL wrong!! Ok, a couple of the properties belong to the StartInfo, but that's what you get coding from memory!!

The rest is correct, and you obviously managed to figure it out for yourself, so hey maybe you learned something.
0
 
VMthinkerAuthor Commented:
The error still remains?.....
0
 
VMthinkerAuthor Commented:
No complete codes were given.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now