c#嵌套CMD窗口
2024-09-22 02:38:56
解决方法一:
自己放一个文本框,改成黑色,然后输入命令,执行时,你Process.Start cmd ,此时CMD窗口不显示,然后,将CMD的返回值,再取出来,设回文本框。
如何用这种方法实时获取cmd返回的数据,简单实现如下
1 private void OutPutForm_Shown(object sender, EventArgs e)
2 {
3 Control.CheckForIllegalCrossThreadCalls = false;
4 process = new Process();
5 p.StartInfo.FileName = "cmd.exe";
6 p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
7 p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
8 p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
9 p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
10 p.StartInfo.CreateNoWindow = true;//不显示程序窗口
11 process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
12 process.Start();//启动程序
13 process.BeginOutputReadLine();
14 }
15 private void OutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
16 {
17 if (!String.IsNullOrEmpty(outLine.Data))
18 {
19 StringBuilder sb = new StringBuilder(this.textBox1.Text);
20 this.textBox1.Text = sb.AppendLine(outLine.Data).ToString();
21 this.textBox1.SelectionStart =this.textBox1.Text.Length;
22 this.textBox1.ScrollToCaret();
23 }
24 }
解决方法二:
直接上代码
1 [DllImport("User32.dll ", EntryPoint = "SetParent")]
2 private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
3 [DllImport("user32.dll ", EntryPoint = "ShowWindow")]
4 public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
5 private void button3_Click(object sender, EventArgs e)
6 {
7 Process p = new Process();
8 p.StartInfo.FileName = "cmd.exe ";
9 p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized;//加上这句效果更好
10 p.Start();
11 System.Threading.Thread.Sleep(100);//加上,100如果效果没有就继续加大
12
13 SetParent(p.MainWindowHandle, panel1.Handle); //panel1.Handle为要显示外部程序的容器
14 ShowWindow(p.MainWindowHandle, 3);
15 }
备注:记得引用 using System.Runtime.InteropServices;
最新文章
- Sublime Text 3103 Crack 破解 注册码(亲测有效)
- 【python】any()和all()
- Sql 解释
- 分享一个在线制作GIF格式loading图片的好网址
- ubuntu 12.04 安装snort acidbase相关注意事项
- Vue 2.0初学后个人总结及分享
- Centos下安装git的web服务器
- DB2 存储过程创建、系统表
- ORM(四)字段参数
- python之正则表达式和re模块一
- SQLServer 账户当前被锁定
- Python for循环之图像练习
- IDEA开发环境中快捷键与系统 QQ等冲突的解决办法
- D.Starry的神奇魔法(矩阵快速幂)
- React+ES6+Webpack深入浅出
- 【C语言】 8421BCD码与二进制的转换
- 基于Vue-cli 快速搭建项目
- JVM垃圾回收--年轻代、年老点和持久代
- [转] 禁止ultraedit自动检查更新的方法
- 支付宝PC网站接口对接
热门文章
- kubernetes 实践四:Pod详解
- 【C#】上级实验四
- SpringCloud之Zuul网关简介、映射vs过滤配置使用
- ORACLE数据库实现自增的两种方式
- Navicat 连接mysql 报错: Authentication plugin caching_ sha2_password cannot be loaded
- ADO.NET 五(DataAdapter 与 DataSet)
- PHP 的一款http请求封装类
- 原生JS获取HTML DOM元素的8种方法
- 1+x学习日志——js获取随机颜色的几种实现方式
- vue动态加载图片