我可以在Windows下发送一些文字到活动进程的STDIN? [英] Can I send some text to the STDIN of an active process under Windows?

查看:145
本文介绍了我可以在Windows下发送一些文字到活动进程的STDIN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了该问题的网站和登陆服务器故障:

I searched the web for that question and landed on Server Fault:

<一个href=\"http://serverfault.com/questions/178457/can-i-send-some-text-to-the-stdin-of-an-active-process-running-in-a-screen-sessi\">Can我发送一些文字到活动进程运行的STDIN在屏幕会话?

Can I send some text to the STDIN of an active process running in a screen session?

好像很可笑容易在Linux下实现这一目标。但我需要它一个Win32命令提示符。

Seems like it is ridiculously easy to achieve this under Linux. But I need it for a Win32 Command Prompt.

背景:我有一个应用程序,民意调查STDIN如果我preSS的<大骨节病> X 键,应用程序终止。现在,我希望做一些自动化测试,测试应用程序,然后将其关闭。

Background: I have an application that polls STDIN and if I press the x key, the application terminates. Now, I want to do some automated testing, test the application and then shut it down.

注意:只要杀死进程是不是因为我目前正在调查我的应用程序的关闭过程中出现的问题的选项。

Note: Just killing the process is not an option since I'm currently investigating problems that arise during the shutdown of my application.

推荐答案

.NET框架的流程和<一个href=\"http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx\">ProcessStartInfo类可用于创建和控制的方法。由于Windows PowerShell中可以用来实例化.NET对象,几乎控制过程的各个方面的能力,可以在PowerShell中。

.NET framework's Process and ProcessStartInfo classes can be used to create and control a process. Since Windows PowerShell can be used to instantiate .NET objects, the ability to control almost every aspect of a process is available from within PowerShell.

下面是你如何可以发送 DIR 命令到的cmd.exe 程序(请确保包裹这名为.ps1文件,然后执行该脚本):

Here's how you can send the dir command to a cmd.exe process (make sure to wrap this in a .ps1 file and then execute the script):

$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "cmd.exe"; #process file
$psi.UseShellExecute = $false; #start the process from it's own executable file
$psi.RedirectStandardInput = $true; #enable the process to read from standard input

$p = [System.Diagnostics.Process]::Start($psi);

Start-Sleep -s 2 #wait 2 seconds so that the process can be up and running

$p.StandardInput.WriteLine("dir"); #StandardInput property of the Process is a .NET StreamWriter object

这篇关于我可以在Windows下发送一些文字到活动进程的STDIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆