本文实例讲述了C#调用dos窗口获取相关信息的方法。分享给大家供大家参考。具体实现方法如下:
/// <summary>/// 调用dos窗口获取相关信息/// </summary>/// <param name="cmd">如:netstat-ano或者ipconfig</param>/// <returns></returns>static string GetCode(string cmd){ ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe"); startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true;//不显示窗口 Process process = new Process(); process.StartInfo = startInfo; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.Start(); process.StandardInput.WriteLine(cmd); process.StandardInput.WriteLine("exit"); string netMessage = process.StandardOutput.ReadToEnd(); process.WaitForExit(); process.Close(); return netMessage;}/// <summary>/// 将获取的信息输出到桌面/// </summary>static void InvokeCode(){ string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "//" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt"; System.IO.File.WriteAllText(path, GetCode("ipconfig"));}
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答