以C#形式嵌入的Powershell或CMD窗口 [英] Powershell or CMD window embedded in C# form

查看:468
本文介绍了以C#形式嵌入的Powershell或CMD窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个我似乎找不到答案的问题,希望有人可以提供帮助:)
我想创建一个类似于Yakuake,Tilda等的应用程序.

目前,我已经能够通过使用文本框输入命令并使用另一个文本框使用流显示结果来实现类似的效果,但这现在还不是真正的shell.

Console2( http://sourceforge.net/projects/console/ [

Hello

I have a question i can''t seem to find an answer to, and i hope someone can help :)
I want to create a application similar to Yakuake, Tilda, etc.

Currently i have been able to achive something simelar to this by using a textbox to enter the commands and another textbox to display the results using streams, but thats not a real shell now is it.

Console2(http://sourceforge.net/projects/console/[^]) does pretty much what I want to acheive, but not 100%.

So is there anyone here that know what technique that have been used in Console2? (And similar programs?) Another program that does this is the Powershell ISE, it''s divided in 3 sections with a interactive powershell-shell at the bottom(So it should be do-able to do this in other programs aswell, right?)

Anyways, I hope someone here are able to put me in the right direction :)


Sincerely
Karl-Einar

推荐答案

这是一个众所周知的东西,它在类System.Management.Automation.PowerShell中实现,请参见:
http://msdn.microsoft.com/zh-cn/library/system.management.automation.powershell%28v = vs.85%29.aspx [ http://www.dougfinke .com/blog/index.php/2010/04/08/how-to-embed-powershell-within-ac-application/ [ Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
This is a well-known thing, which is implemented in the class System.Management.Automation.PowerShell, please see:
http://msdn.microsoft.com/en-us/library/system.management.automation.powershell%28v=vs.85%29.aspx[^].

You can find a simple code sample here:
http://www.dougfinke.com/blog/index.php/2010/04/08/how-to-embed-powershell-within-a-c-application/[^].

Now, this sample uses a default console as it comes in console applications. You can use console in your Forms applications; you can show no output; or you can show output in some System.Windows.Forms control. I would recommend using ListBox to have the look closest to the traditional console. You can present the work of the shell in any other way.

If the execution of the shell can take some noticeable time, you should run it all in a separate thread. If you need to present some data produced by this PowerShell thread, you will need to use UI thread invocation, which I described in detail in my past solutions:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references to my past answers on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于以C#形式嵌入的Powershell或CMD窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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