我可以从Windows窗体调用控制台应用程序主方法。 [英] Can I call a console application main method from windows form.

查看:72
本文介绍了我可以从Windows窗体调用控制台应用程序主方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从控制台应用程序添加了一个现有的项目到我的Windows窗体项目。我想知道是否有任何方法从windows窗体调用控制台应用程序主方法。我已经将2项目添加到我的解决方案中(例如当我按下按钮。)教程,例子......



谢谢







I added an existing project from console application to my windows form project.I would like to know if is any way to call the console application main method from windows form.I have already add the 2 project to my solution(For example when i press a button.)tutorials,examples...

Thank you



private void button1_Click(object sender, EventArgs e)
{
 //call main method



}





我尝试了什么:



我不知道是否有可能。我还没有找到任何东西internet。



What I have tried:

I don t know if is it possible.I haven't found anything over the internet.

推荐答案

如果您的目的是使用Windows窗体中的现有应用程序启动新进程,此解决方案可能是正确的给你。



在这种情况下你可以使用Process类来启动另一个应用程序。

参见进程类(System.Diagnostics) [ ^ ]



如果你阅读文档,你会发现一些很好的例子。



如果你想在启动时传输参数你应该查看 ProcessStartInfo类(System.Diagnostics) [ ^ ]并使用属性Arguments。

参数应该用空格分隔。



在你的conso中le application你使用main方法的args参数。

If your intention is to start a new process using an existing application from within the Windows Form, this solution might be right for you.

In that case you can use the Process class to start the other application.
See Process Class (System.Diagnostics)[^]

If you read the documentation you will find some pretty good examples.

If you want to transfer parameters at startup you should look into the ProcessStartInfo Class (System.Diagnostics)[^] and use the property Arguments.
The arguments should be separated with a space.

In your console application you use the args parameter of the main method.
static void Main(string[] args)
{
    foreach (string arg in args)
    {
        // Do something
    }
}


我不知道你为什么要调用'main'方法本身 - 如果我必须将控制台程序的功能集成到WinForms程序中我会



a)在Winforms项目中定义一个新的静态函数,例如xyz_main



b)将控制台主代码行中的行复制到新函数xyz_main



c)让你的button1_Click处理程序调用xyz_main



d)让它工作(修复r) (等等)然后



e)重构需要更好地利用WinForms项目 - 在WinForms表单上使用文本框获取结果,输入等



这是一个粗略的指南 - 这实际上取决于控制台项目的main()当然是多少涉及
I dont know why you'd call the 'main' method per se - if I had to do integrate functionality from a console program into a WinForms program I would

a) define a new static function within the Winforms Project eg xyz_main

b) copy the lines from the console main code line into new function xyz_main

c) have your button1_Click handler call xyz_main

d) get it working (fix references) etc then

e) refactor as required to make better use of a WinForms project - use textboxes on WinForms Form for results, input etc

Thats a rough guide - it really depends how involved the main() of the console project is of course


这篇关于我可以从Windows窗体调用控制台应用程序主方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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