简体   繁体   English

如何创建命令应用程序,例如为 vista 运行命令

[英]how to create command application like run command for vista

how to create application like window run command using C#.如何使用 C# 创建像 window 运行命令这样的应用程序。 When i insert any command (for example: ipconfig), this return result (for example: 192.168.1.1) on the textbox.当我插入任何命令(例如:ipconfig)时,此文本框会返回结果(例如:192.168.1.1)。

  1. how to get windows command list?如何获取 windows 命令列表?
  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%. (1) 命令列表很可能来自在您的 %PATH% 中找到的任何可执行文件。 You can figure out your list by finding all.exe/.bat/other executable files in every folder specified by %PATH%.您可以通过在 %PATH% 指定的每个文件夹中查找 all.exe/.bat/other 可执行文件来找出您的列表。 You may not even need to know which apps are available, the Process.Start method will find them for you.您甚至可能不需要知道哪些应用程序可用,Process.Start 方法会为您找到它们。 (see below) (见下文)

(2) You can run a command-line tool programmatically using: (2) 您可以使用以下方式以编程方式运行命令行工具:

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

To capture the output, you have to redirect it like this:要捕获 output,您必须像这样重定向它:

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 (3) 可能与1相同的答案

Create a Windows Forms application using the wizard.使用向导创建 Windows Forms 应用程序。 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.将 Click 处理程序添加到获取文本框内容并启动进程的按钮。 Use the Process class.使用过程class。 That class also has a StandardOutput property that you can read the output from so you can put it into the text box. class 还具有StandardOutput属性,您可以从中读取 output,以便将其放入文本框中。

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.您可能会发现要使用许多命令提示符命令,您需要在前面键入CMD /C ,因为它们不是与命令解释器分开的程序。

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).命令只是一个程序(或CMD命令解释器的一个功能)。 You could search the hard drive for .exe files, but then many of them wouldn't be suitable as "commands".您可以在硬盘驱动器中搜索.exe文件,但其中许多文件不适合用作“命令”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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