How to execute DOS command and retrieve result [duplicate] - c#

This question already has answers here:
How To: Execute command line in C#, get STD OUT results
(17 answers)
Closed 9 years ago.
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.

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

Don't do that.
Use DirectoryInfo.

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.

Related

Reading from Standard Input in C# [duplicate]

This question already has answers here:
C# Console receive input with pipe
(8 answers)
Closed 7 years ago.
I'm trying to read in a text file from the command prompt in C#, via
program.exe < textfile.txt
However, I cannot find the correct way to do this.
So far, I've only been able to pass the path and the filename to string [] args and then opening the file with the StreamReader class. While this is an acceptable alternative, I've been told that the method with "<", which I suppose is a redirection of standard input, offers advantages like not requiring file handling.
Can anyone give some insight into this?
edit: Program.exe is my C# application.
You've got the right idea - the '<' symbol means the Console class reads from the file you specify, instead of reading user input from the console. When you do this, you read from it using the Console class.
The advantages of reading from STDIN is that the user can either run the program as program.exe, and manually type the input the program is expecting, or they can run it as program.exe < input.txt. The only time this is a disadvantage is if you know you will always supply a file, and consider the effort of typing the '<' symbol too much...
In a command prompt the < sign is one of several redirection operators. Specifically, the < sign is the input redirection operator. When you type this: program.exe < textfile.txt, you are telling the command prompt to execute program.exe and read the command input from a file, instead of reading input from the keyboard. In this way, the command prompt basically opens textfile.txt and takes its content and "stuffs it" into the keyboard buffer, so, as far as program.exe is concerned, the input is being read from the keyboard and has no idea you are actually "stuffing" the keyboard buffer with contents from a file.
If your program currently is reading from a file, you will need to modify your program. You no longer want to read from a file and instead read from the keyboard, using commands such as Console.ReadLine or Console.Read or Console.ReadKey.
As far as advantages, the advantages are minimal.

how to find path to folder in c#? [duplicate]

This question already has answers here:
Relative path to absolute path in C#?
(8 answers)
Closed 8 years ago.
My main function takes arguments from command line.
This arguments looks like
--args ../../TempImages ../Resources.csproj etc.
How can i find full path to this folder in c# from this arguments?
Like
Users/%username%/Projects/Resource/Resource.csproj
I tried use Path, Directory, Enviroment classes, but nothing helps me
I am using Xamarin, MacOSX 10.9
EDIT:
Forget to say, that i don't know fullpaths as provided above. My script are running on different systems. So, fullpath to these files can be different.
Moriarty's answer helps me, but with this condition, it doesn't work.
Luckily Mono has adopted this quite well in the System.IO space. You're in the right direction with the Environment classes, like so:
var path = Path.Combine (
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Projects",
"Resource",
"Resource.csproj");
The Environment.GetFolderPath, in combination with Environment.SpecialFolder.UserProfile, will do the trick to fetch what is known in Windows as %USERPROFILE%.
Second, notice I've split the /Projects/Resource/ part in seperate arguments for the Path.Combine function. This is to prevent issues with Windows<>Mac<>Linux file operations and let mono figure this out for you.
So this'll work on all platforms. Enjoy!
Update:
For the downvoters that consider this to be a wrong answer: the suggestion mentioned in the referenced duplicate article does not work on Mac or Linux. This is because "/paths/with/slahes" act differently on Mono/Xamarin platform in non-Windows environments. And this is what the original question was about. #imho

How to launch a JVM from C#

I would like to launch a java application from C# but am unsure of the proper way to do it. I can do it from a bat file:
java -cp ".;other_classes_location" classname
How does this get turned into C#?
What you have posted is a command line (likely executed in a command shell)
you cannot turn this into C# code
I guess you are looking for a way to execute that command using C#. In this case the answer is the below code
System.Diagnostics.Process.Start("yourPath\java.exe", "Command Line Arguments");
Use Process.Start.
Examples here.

How to run a command line on a remote computer using C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to execute a command in a remote computer?
here is my question.
I have a remote computer LIQIANDEV and on it there is a command C:\test\test.cmd. Now I have a web server LIQIANTEST, I want to run the test.cmd on remote computer LIQIANDEV using C# code, how should I do this?
Thanks a lot for your help.
You can run a program on another computer using PsExec, which is part of Microsoft's SysInternals. Therefore, you could use c# to create a local process that runs the PsExec command.
Alternatively, you can use Windows PowerShell Remoting to run a powershell script on another computer. Once again, this could be kicked off using c#.
I think you can also do it with WMI or perhaps even RDP.
Short answer: you can't. There are all kinds of barriers in place to prevent you from being able to do exactly that.
Longer answer: If you explain what you're trying to accomplish by running this code, there might be a better way to do it.

Run an executable present in Windows Path using C#

I'm trying to run some commands, like rails test, using a C# command line. I tried using How To: Execute command line in C#, get STD OUT results but I'll need full path to the rails executable for that to work. Is there any alternative that will find work just like the windows command line does?
If you can P/Invoke, you could locate the executable with PathFindOnPath. A quick google doesn't show a C# equivalent.
without P/Invoke, Environment.GetEnvironmentVariable("Path").Split(";") should give you a list of paths to probe.
However, this is not the entire resolution used by ShellExecute or even the console.
I believe if you have UseShellExecute set to true in the ProcessStartInfo used to start the process, it'll use the path. Haven't checked it yet - will do so when I get a chance.

Categories