2

My C# application requirement is to issue dir command from command line (with J:\MyFolder> as current directory) and receive output from that command in my C# application.

I tried MSDN where issuing command line examples are there like "/c dir" but I would like to retrieve result also.

Can somebody help me with that? Thanks in advance.

Prix
  • 19,417
  • 15
  • 73
  • 132
user2680162
  • 21
  • 1
  • 2
  • 4
    please see this: http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-results – Jubal Aug 13 '13 at 21:02
  • 12
    Don't do that. Use `DirectoryInfo`. – SLaks Aug 13 '13 at 21:03
  • I do not want to use DirectoryInfo as I am going to issue host of other commands. So it may not be useful – user2680162 Aug 13 '13 at 21:25
  • @user2680162 ok, so issue the `DirectoryInfo` for the "dir" and use `Process` for the rest of your commands. – Scott Chamberlain Aug 13 '13 at 22:00
  • @user2680162 Please note that capturing the output stream "the simple" way, does not support large amount of data, and can result in a **deadlock**, if you need a sample code for capturing large amounts of data from the command result, reply to this comment... – Nir Kornfeld Aug 13 '13 at 22:17

3 Answers3

1

http://www.dotnetperls.com/process-start and http://www.dotnetperls.com/redirectstandardoutput

Y Ou can use the ProcessStartInfo class to call other exes and .bats etc and redirect the outout and errors back into your c# program. However this is nasty and could be better achieved in direct code probably depending on what logic you are calling in addition to yoir example. Thinhs such as powershell and wmi and basical file and directory handling

Kyle
  • 951
  • 3
  • 14
  • 31
  • I appreciate your response. However both examples use .exe file execution, not direct commandline execution. Please let me know if you have any such example. – user2680162 Aug 13 '13 at 21:55
  • In the .start () you could do .start ("cmd.exe /C dir") where dir is a command that you could run in a command window. Google what flags cmd.exe has. Depending how complex yours is may determine if this should be done in code which I strongly advise. Once you get the response back you will have to format it with string manipulation. Yuck. – Kyle Aug 13 '13 at 22:00
1

Don't do that.

Use DirectoryInfo.

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

The basic idea is to start a process using "command.com" as the executable and pass the command line as a parameter as well as redirect the stdout back into your program.

I just reviewed these two links: http://www.dotnetperls.com/process-start and http://www.dotnetperls.com/redirectstandardoutput These are reasonable descriptions of what you need to do.

It really isn't that difficult, mostly just time consuming getting all the details correct!

To test this out prior to programming invoke command.com and ensure you can use it to issue commands. I found my copy of command.com at C:\WINDOWS\system32.

Hope this helps, please ask if more questions.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21