• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

ASP.net - Executing command line file

Reel

Diamond Member
Can anybody point me in the right direction to do this? I'd like to have a simple web page with a "Go" button. When the go button is clicked, the post action kicks off some command line program, say "dir c:\" and reports the output.

I would not be adverse to a recommendation to switching languages to PHP. Thanks.
 
Originally posted by: joshsquall
What you want to do would be much easier handled by the System.IO.DirectoryInfo class.

I'm sorry. Maybe that was a bad example of the command line program. I was just throwing out an arbitrary program that we could all relate to. The actual program is something that will not be simplified through a class such as dir. Thanks.
 
Originally posted by: Reel
Originally posted by: joshsquall
What you want to do would be much easier handled by the System.IO.DirectoryInfo class.

I'm sorry. Maybe that was a bad example of the command line program. I was just throwing out an arbitrary program that we could all relate to. The actual program is something that will not be simplified through a class such as dir. Thanks.

Inline DOS commands, or will it be a batch file?
 
Originally posted by: joshsquall
Originally posted by: Reel
Originally posted by: joshsquall
What you want to do would be much easier handled by the System.IO.DirectoryInfo class.

I'm sorry. Maybe that was a bad example of the command line program. I was just throwing out an arbitrary program that we could all relate to. The actual program is something that will not be simplified through a class such as dir. Thanks.

Inline DOS commands, or will it be a batch file?

I only have one command (with some parameters) to execute.
 
I'm not sure, but I believe you should be able to put the command into a batch file and use System.Diagnostics.Process to start it. Have the command in the batch file redirect it's output to a file (command -parameters > out.txt) and read this file in .NET to print the results to the screen.
 
Originally posted by: joshsquall
I'm not sure, but I believe you should be able to put the command into a batch file and use System.Diagnostics.Process to start it. Have the command in the batch file redirect it's output to a file (command -parameters > out.txt) and read this file in .NET to print the results to the screen.

Thanks. I will give this a shot.
 
I've used a socket listener to do something similar. Do you have control over the code that will be executed by the command?
 
Originally posted by: GoatMonkey
I've used a socket listener to do something similar. Do you have control over the code that will be executed by the command?

I will have full control over every aspect: code, web server, system, etc.
 
You'll probably have to set the trust to full at the machine.config level to get the right to execute a process like that.
 
This little bit of code worked, interestingly without much security configurations either (probably because I'm on the same domain as the web server)

System.Diagnostics.Process proc;
System.Diagnostics.ProcessStartInfo procInfo = new System.Diagnostics.ProcessStartInfo();

procInfo.UseShellExecute = true; // If this is false, only .exe's can be run.
procInfo.WorkingDirectory = "C:";
procInfo.FileName = "c:\\test.bat"; // simple batch file which has command line instructions, dir *.* etc
procInfo.Arguments = " > c:\\out.txt"; // output to file
procInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

proc = System.Diagnostics.Process.Start(procInfo);
proc.WaitForExit(); // Waits for the process to end.

if(!proc.HasExited)
{
proc.Kill();
ReadFile(); // read in file/do what you want with it
}
 
Thanks for that detailed code Snapster. That really helped. I was able to get it going from that.
 
Back
Top