【踩大坑指南】CSharp_Process_运行之后提示:No Such File或者PyInstaller提示BadZip等

最终程序概览

在这里插入图片描述

帮辅导员写的一个程序,用于检测提交上来的行程码有没有出现“*”号。

问题描述

在今天的编写一个小程序的时候,发现了一个问题,那就是,调用Python程序的exe会提示一个很恶心的错误:
在这里插入图片描述

解决思路

非常简单
就是因为
Process没有办法找打目录
所以加一行:
CmdProcess.StartInfo.WorkingDirectory = "RunTime"; //必须要指定路径,要不然会运行失败
解决了

异步输出IO流

请看下方详细代码:

        private void RealAction(string StartFileName) {
            CmdProcess = new Process();
            CmdProcess.StartInfo.FileName = StartFileName;      // 命令  

            CmdProcess.StartInfo.CreateNoWindow = false;         // 不创建新窗口  
            CmdProcess.StartInfo.UseShellExecute = false;
            CmdProcess.StartInfo.RedirectStandardInput = false;  // 重定向输入  
            CmdProcess.StartInfo.RedirectStandardOutput = true; // 重定向标准输出  
                                                                //CmdProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;  

            CmdProcess.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);

            CmdProcess.EnableRaisingEvents = true;                      // 启用Exited事件  
            CmdProcess.Exited += new EventHandler(CmdProcess_Exited);   // 注册进程结束事件  

            CmdProcess.StartInfo.WorkingDirectory = "RunTime"; //必须要指定路径,要不然会运行失败

            CmdProcess.Start();
            CmdProcess.BeginOutputReadLine();
        }

        private void p_OutputDataReceived(object sender, DataReceivedEventArgs e) {
            if (e.Data != null) {
                //切换不同线程 上下文操作
                //TextBlock_Result.Text += e.Data + "\n";
                Dispatcher.Invoke(() => {
                    string str = e.Data;
                    //TextBlock_Result.Text += e.Data + "\n";
                    if (str.Contains("[1;31;40m")) {
                        Panel.Children.Add(new TextBlock() { Text = "注意!这张好像检测出了*号!", FontSize = 18, Margin = new Thickness(10, 0, 10, 0), TextWrapping = TextWrapping.Wrap, Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0)) });
                    } else {
                        Panel.Children.Add(new TextBlock() { Text = str, FontSize = 18, Margin = new Thickness(10, 0, 10, 0), TextWrapping = TextWrapping.Wrap });
                    }

                });  //这是wpf需要用dispatcher,如果是winform项目是this.
            }
        }

        private void CmdProcess_Exited(object sender, EventArgs e) {
            if (CmdProcess != null) {
                CmdProcess.Close();
                alreadyRunCmd = false;
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值