简体   繁体   English

如何调用随主应用程序一起分发的.NET exe

[英]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: 正在考虑的选项:

  1. Should I add the exe as a file within the project and set Copy to Output as 'Copy always' and then just run it from the main application folder? 我是否应该将exe作为文件添加到项目中,并将“复制到输出”设置为“始终复制”,然后从主应用程序文件夹运行它?
  2. Should I add the exe as a reference in the main project? 我应该在主项目中添加该exe作为参考吗? If I do this how would I then call it as an executable with parameters? 如果执行此操作,然后如何将其作为带有参数的可执行文件调用?

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文件中的代码:

  • As a separate process: use System.Diagnostics.Process and pass in any information as command line arguments 作为一个单独的过程:使用System.Diagnostics.Process并将任何信息作为命令行参数传递
  • In the same process, using normal .NET method calls 在同一过程中,使用常规.NET方法调用
  • In the same process in a separate 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.

相关问题 我应该如何在.NET中实现分布式远程方法调用系统? - How should I implement a distributed Remote Method Invocation system in .NET? 如何将dll项目设置整合到Visual Studio 2010中用于C#应用程序的主exe.config文件中? - How can I consolidate my dll project settings into my main exe.config file in Visual Studio 2010 for a C# application? 如何在我的 C# 应用程序中调用第三方 exe? - How to call an third party exe within my C# application? 如何在项目中配置其他exe文件的路径? - How should I configure paths to other exe files in my project? 如何在分布式应用程序中使用.Net WorkFlow 4.0 - How to use .Net WorkFlow 4.0 in distributed application .NET 怎么知道我的应用程序的主要方法是什么? - How does .NET know what the main method of my application is? 我应该如何保护我的 Web 应用程序(ASP.Net Core 3.1 MVC)? - How should i secure my Web Application(ASP.Net Core 3.1 MVC)? .NET分布式分层应用程序 - .NET distributed layered application 我应该为我的应用程序使用C#/ .Net还是C ++? - Should I go with C# / .Net OR C++ for my application? 我如何在asp.net Web应用程序中调用嵌入dll中的javascript文件? - How do I call a javascript file that's embedded in a dll in my asp.net web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM