简体   繁体   中英

how to create command application like run command for vista

how to create application like window run command using C#. When i insert any command (for example: ipconfig), this return result (for example: 192.168.1.1) on the textbox.

  1. how to get windows command list?
  2. how to get command result?
  3. how to get installed application list on the machine?

(1) The command list will most likely come from whatever executables are found in your %PATH%. You can figure out your list by finding all.exe/.bat/other executable files in every folder specified by %PATH%. You may not even need to know which apps are available, the Process.Start method will find them for you. (see below)

(2) You can run a command-line tool programmatically using:

System.Diagnostics.Process.Start("notepad.exe"); // located using %PATH%

To capture the output, you have to redirect it like this:

System.Diagnostics.ProcessStartInfo psi =
    new System.Diagnostics.ProcessStartInfo(@"ipconfig");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;

System.Diagnostics.Process myProcess;
myProcess = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = myProcess.StandardOutput; // Capture output
myProcess.WaitForExit(2000);
if (myProcess.HasExited)
{
    string output = myOutput.ReadToEnd();
    Console.WriteLine(output);
}

(3) Probably the same answer as 1

Create a Windows Forms application using the wizard. Draw a text box and a button. Add a Click handler to the button which takes the contents of the text box and launches a process. Use the Process class. That class also has a StandardOutput property that you can read the output from so you can put it into the text box.

You may find that to use many Command Prompt commands, you need to type CMD /C in front, because they aren't separate programs from the command interpreter.

As for discovering a list of commands, that's not generally possible. A command is just a program (or a feature of the CMD command interpreter). You could search the hard drive for .exe files, but then many of them wouldn't be suitable as "commands".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM