CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Question Alternative to shell command ?

    Hello all,

    I want to know what is the alternative to shell command in C# ?
    Below is the shell command.

    Code:
    shell("C:\Compute.exe" + " Parse" + " C:\source.txt")
    I have a 3rd party console application named "Compute.exe", which accepts a text file as input.

    so Compute.exe is the application i call from my application.
    Here "Parse" is one of the commands i pass depending upon the need.
    And Source.txt is the name of the input file.

    at present i am calling this program using Process.Start method. But this doest open/run the compute.exe as it should run when i double click on it.

    also i cannot pass the parameters to this file.

    Need serious help in this regards.

    thanks
    MMH
    Regards,
    MMH
    Rate my post if you find it usefull.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Alternative to shell command ?

    Does it work manually? You could try to write a batch file, and call that.
    You could even write a batch file on the fly
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Question Re: Alternative to shell command ?

    Quote Originally Posted by dglienna
    Does it work manually? You could try to write a batch file, and call that.
    You could even write a batch file on the fly
    Can you explain how to achieve this using batch file ?

    The problem i am facing is,

    The behavior of compute.exe is different when opened with process.start. and different when opened manually (with double click).

    Let me explain you this behavior:
    When i run this exe manually (with double click) or through command prompt, i could see the compute.exe to ask for the input source file.

    But when i run compute.exe using process.start, it just opens the console window. and does not ask for the source file (i can only see a blank console window)

    what is the problem with this ? do i need to call/ run compute.exe with some extra code ?

    Code:
    private void bkgWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
    	Process startCompiler = new Process();
    	startCompiler.StartInfo.FileName = string.Concat(Application.StartupPath, "\\Compute.EXE");
    	startCompiler.Start();
    	startCompiler.WaitForExit();
    }
    private void bkgWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    { 
       MessageBox.Show("Command executed.");
    }


    I cannot figure out how to pass parameters to the exe.
    Regards,
    MMH
    Rate my post if you find it usefull.

  4. #4
    Join Date
    May 2007
    Posts
    811

    Re: Alternative to shell command ?

    StartInfo member of Process has Arguments property. Look it up on MSDN.

  5. #5
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Thumbs up Re: Alternative to shell command ?

    Quote Originally Posted by dglienna
    Does it work manually? You could try to write a batch file, and call that.
    You could even write a batch file on the fly
    Thanks for the suggestion... the batch file system works.... now i have to create a batch file on runtime first..... n call it from my application....

    thanks alot once agian...
    Regards,
    MMH
    Rate my post if you find it usefull.

  6. #6
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762

    Re: Alternative to shell command ?

    Quote Originally Posted by MMH
    at present i am calling this program using Process.Start method. But this doest open/run the compute.exe as it should run when i double click on it.
    Not sure what you mean. What do you mean it doesn't run as if you double clicked on it?

    Quote Originally Posted by MMH
    also i cannot pass the parameters to this file.
    Sure you can. The second parameter to the Process.Start() method are the parameters.

    So if you're used to doing this:
    shell("C:\Compute.exe" + " Parse" + " C:\source.txt")

    You would now do this:
    Process.Start("C:\\Computer.exe", "C:\\source.txt");

    Since you're using StartInfo, you would add the parameters to the Arguements property as STLDude suggested.
    Quote Originally Posted by MMH
    Thanks for the suggestion... the batch file system works.... now i have to create a batch file on runtime first..... n call it from my application....

    thanks alot once agian...
    Why would you want to constantly create batch files? That's additional overhead your application doesn't need.

  7. #7
    Join Date
    Jan 2006
    Location
    18° 32' N / 73° 52' E
    Posts
    416

    Exclamation Re: Alternative to shell command ?

    Quote Originally Posted by kasracer
    Not sure what you mean. What do you mean it doesn't run as if you double clicked on it?
    When i double click on the file, it gets open in the correct way. because it askes for the parameters(i.e. the source file name).

    But when i try to open the same file using process.start, it opens the file, but i can only see a blank "black" window. the file does not ask me for the input parameter.
    Basically it looks like it is waiting for something....

    here i am looking for the alternative to the shell command in C#.

    Quote Originally Posted by kasracer
    You would now do this:
    Process.Start("C:\\Computer.exe", "C:\\source.txt");
    i am not sure if the above code works in C#...

    Quote Originally Posted by kasracer
    Since you're using StartInfo, you would add the parameters to the Arguements property as STLDude suggested.
    the console application does not accept parameters this way.

    Quote Originally Posted by kasracer
    Why would you want to constantly create batch files? That's additional overhead your application doesn't need.[/
    Well creating batch file is just an alternative solution that i can think of...
    Last edited by MMH; December 25th, 2007 at 01:35 AM.
    Regards,
    MMH
    Rate my post if you find it usefull.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured