[英]Invoking a second Visual Studio project programmatically
I'm working on a solution in Visual Studio 2010 Ultimate that contains two C# projects: a Forms project and a console project. 我正在研究包含两个C#项目的Visual Studio 2010 Ultimate中的解决方案:Forms项目和Console项目。 Programmatically, they are completely separate;
从编程上讲,它们是完全独立的; the main portion of both projects could run without the other.
两个项目的主要部分可以在没有其他项目的情况下运行。 What I would like to do is invoke the console application by pressing a button in the Forms application, with certain options set based on the Forms application.
我想做的是通过在Forms应用程序中按下按钮来调用控制台应用程序,并基于Forms应用程序设置了某些选项。 So you can think of the Forms application as essentially just a 'launcher' app.
因此,您可以将Forms应用程序视为本质上只是一个“启动器”应用程序。
The way I have this implemented right now is by using System.Diagnostics.Process.Start
with "cmd"
as the first parameter and the path to the console application's executable (plus CLI arguments) as the second parameter. 我现在实现此方法的方式是使用
System.Diagnostics.Process.Start
,将"cmd"
作为第一个参数,并将控制台应用程序可执行文件的路径(加上CLI参数)作为第二个参数。 But this is a terrible solution, because it will break when deployed to other machines with an installer. 但这是一个糟糕的解决方案,因为当使用安装程序将其部署到其他计算机时,它会中断。
I've also tried creating a reference to the console project in the Forms object, making Program
in the console project public and static, and calling the console project's Main
method, but because there's no valid output handle, this eventually causes an exception to be thrown. 我还尝试过在Forms对象中创建对控制台项目的引用,使控制台项目中的
Program
为公共和静态,并调用控制台项目的Main
方法,但是由于没有有效的输出句柄,最终会导致异常抛出。
I've done much Googling, some searching here on SO, and even checked the 'Similar Questions' on the submission page, and no one seems to know how to do what I want to do. 我做了很多谷歌搜索,在SO上进行了一些搜索,甚至在提交页面上检查了“类似问题”,似乎没人知道如何做我想做的事情。 Any pointers that could put me on the right track?
有什么指针可以使我走上正确的轨道吗?
FYI: I know that there's probably ten million better ways to do this, but I have several reasons for wanting and needing it done this way. 仅供参考:我知道可能有上千万种更好的方法可以做到这一点,但是我有几个理由希望这样做。 I know I could just make a separate GUI for the CLI program and copy the relevant code over, or merge everything into one project and make a separate output window, or various other solutions like those.
我知道我可以为CLI程序创建一个单独的GUI,然后复制相关代码,或者将所有内容合并到一个项目中,并创建一个单独的输出窗口,或者诸如此类的其他解决方案。 But I need this solution for two reasons: (1) I had a fairly complex CLI program that works well as a CLI program, and now need a user-friendly way to start it, and (2) solving the problem in other ways doesn't answer the question of "Why bother having several executable projects in the same solution in the first place if you can't invoke one from the other?"
但是由于两个原因,我需要此解决方案:(1)我有一个相当复杂的CLI程序,可以像CLI程序一样很好地工作,现在需要一种用户友好的方式来启动它,并且(2)以其他方式解决问题不会不会回答“如果您不能从另一个调用另一个为什么在同一解决方案中首先拥有多个可执行项目的问题?”
The way you're doing it is fine - it just needs a bit of tweaking. 您的操作方式很好-只需稍作调整即可。
Firstly, when you launch, don't run cmd
with your executable as a parameter, run your console application myapp.exe
directly as the command to execute in Process.Start. 首先,在启动时,不要以可执行文件作为参数运行
cmd
,而是直接将控制台应用程序myapp.exe
作为要在Process.Start中执行的命令来运行。
The next step is to make your application run from wherever it was installed. 下一步是使您的应用程序可以从安装位置运行。 The easiest approach to this is to simply deploy both executables side-by-side in the same folder.
最简单的方法是简单地将两个可执行文件并排部署在同一文件夹中。 Use
Application.StartupPath
to find the folder that your Forms application was launched from (and hence where the other app resides). 使用
Application.StartupPath
查找从中启动Forms应用程序的文件夹(因此找到另一个应用程序所在的文件夹)。 Use Path.Combine(Path.GetDirectory(Application.StartupPath), "myapp.exe")
to generate the full pathname of the exe to run. 使用
Path.Combine(Path.GetDirectory(Application.StartupPath), "myapp.exe")
生成要运行的exe的完整路径名。
Another option is to use an installer that can then store the location of the other app in a known place (eg a registry key), but that's a magnitude more complexity for little gain (unless you need the installer for other reasons). 另一个选择是使用安装程序,该安装程序然后可以将另一个应用程序的位置存储在已知位置(例如注册表项),但这会带来更大的复杂性而几乎没有收益(除非出于其他原因需要安装程序)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.