[英]How should I call a .NET exe that is distributed with my main application
I have a .NET Windows application that needs to call another .NET executable. 我有一个.NET Windows应用程序,需要调用另一个.NET可执行文件。 I want to distribute this other exe as part of the main project, and was wondering what is the best way of implementing this.
我想将此其他exe作为主要项目的一部分进行分发,并且想知道实现此目标的最佳方法是什么。 We don't want to take the code from this second exe and put it in the main project as we need the exe to effectively remain sealed, as we are also distributing it to third parties.
我们不想从第二个exe中获取代码并将其放入主项目中,因为我们需要将exe有效地保持密封状态,因为我们还将其分发给了第三方。
Options being considered: 正在考虑的选项:
I would be grateful for some guidance on the above approaches, and indeed if you have any other ways of achieving my goal. 对于上述方法,以及您有其他实现我的目标的方法,我将不胜感激。
Thanks! 谢谢!
There are two different ways of executing the code in the exe file: 有两种不同的方法来执行exe文件中的代码:
System.Diagnostics.Process
and pass in any information as command line arguments System.Diagnostics.Process
并将任何信息作为命令行参数传递 AppDomain
AppDomain
Both of these work fine; 两者都工作正常; in the first version you wouldn't need your main project to refer to the exe at all: just include it in your setup/deployment project, if you have one.
在第一个版本中,您根本不需要您的主项目完全引用该exe:如果有,只需将其包含在设置/部署项目中即可。 If you're using xcopy deployment (ie just taking the contents of the output directory) then you could add a reference to the exe just to get it copied.
如果您使用的是xcopy部署(即仅获取输出目录的内容),则可以添加对exe的引用只是为了将其复制。
To use the second form you'd need to add a reference to the exe file in your project. 要使用第二种形式,您需要在项目中添加对exe文件的引用。 If you're happy to call the code in-process this is likely to be the simplest solution.
如果您乐于在过程中调用代码,则这可能是最简单的解决方案。
The third form could use a reference but doesn't have to. 第三种形式可以使用引用,但不必使用。 This gives more isolation than the second option but less than the first.
与第二个选项相比,这提供了更多的隔离,但比第一个选项少。
Copy to output is, of course, the simplest solution. 复制到输出当然是最简单的解决方案。
If you don't want to have this exe hanging around together with your app files, put it to resources and when your application needs to run that exe, just save it to temp folder and run from there. 如果您不希望将此exe文件与应用程序文件混在一起,请将其放入资源中,当您的应用程序需要运行该exe文件时,只需将其保存到temp文件夹并从那里运行即可。
Here's proof-of-concept code: 这是概念验证代码:
class Program
{
static void Main(string[] args)
{
var fileName = Path.ChangeExtension(Path.GetTempFileName(), "exe");
File.WriteAllBytes(fileName, Resources.QueryExpress);
var queryAnalyzer = Process.Start(fileName);
queryAnalyzer.WaitForExit();
}
}
In project properties go to Resources and drag-drop there your exe file.
在项目属性中,转到资源,然后将您的exe文件拖放到那里。 This sample is using application named QueryExpress.exe .
此示例正在使用名为QueryExpress.exe的应用程序。
You may also wish to compress your app into ZIP file to make it smaller. 您可能还希望将应用程序压缩为ZIP文件以使其更小。 This will add one more step to unpack archive before running the process.
这将在运行该过程之前增加一个步骤来解压缩存档。
您也可以将其创建为Dll Assembly并从您的应用程序中调用它,这意味着如果运行时存在,则在运行时加载该程序集。
Adding a reference to that exe won't help, unless you want to use classes from the exe in you main program. 除非您要在主程序中使用该exe中的类,否则添加对该exe的引用将无济于事。 I think your first option is the best
我认为您的第一选择是最好的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.