C#代码加注释,谢谢

来源:百度知道 编辑:UC知道 时间:2024/07/08 18:51:19
请加详细的注释以便学习,谢谢
public string CmdPc(string cmdinput)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine(cmdinput);
p.StandardInput.WriteLine("exit");
string ss=p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
return ss;
}
catch
{
string ss = "命令执行失败";
return ss;
}
}

//返回一个系统shell命令控制台输出,比如:
//CmdPc("dir c:\") 会返回"dir c:\"命令的输出结果
public string CmdPc(string cmdinput)
{
//创建一个新的进程
Process p = new Process();
//进程对应的可执行文件是"cmd.exe"
p.StartInfo.FileName = "cmd.exe";
//是否使用操作系统shell执行该程序
p.StartInfo.UseShellExecute = false;
//允许输入重定向,可以写向 "cmd.exe"写入命令
p.StartInfo.RedirectStandardInput = true;
//允许输出重定向,可以获取执行的结果
p.StartInfo.RedirectStandardOutput = true;
//是否创建一个窗口,true表示不创建
p.StartInfo.CreateNoWindow = true;
try
{
//启动进程
p.Start();
////向进程写入 cmdinput命令,即相当于 在cmd.exe中执行 cmdinput
p.StandardInput.WriteLine(cmdinput);
//向进程写入 "exit",即相当于 在cmd.exe中执行 "exit",退出cmd.exe
p.StandardInput.WriteLine("exit");
//ss储存&q