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

发布于 2024-08-04 07:46:38 字数 360 浏览 2 评论 0原文

我有一个 .NET Windows 应用程序需要调用另一个 .NET 可执行文件。我想将另一个 exe 作为主项目的一部分进行分发,并且想知道实现此目的的最佳方法是什么。我们不想从第二个 exe 中取出代码并将其放入主项目中,因为我们需要该 exe 有效地保持密封,因为我们还将其分发给第三方。

正在考虑的选项:

  1. 我是否应该将 exe 添加为项目中的文件并将“复制到输出”设置为“始终复制”,然后从主应用程序文件夹运行它?
  2. 我应该在主项目中添加 exe 作为参考吗?如果我这样做,我将如何将其称为带有参数的可执行文件?

如果您有任何其他方法来实现我的目标,我将不胜感激对上述方法的一些指导。

谢谢!

I have a .NET Windows application that needs to call another .NET executable. I want to distribute this other exe as part of the main project, and was wondering what is the best way of implementing this. 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.

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?
  2. Should I add the exe as a reference in the main project? 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猥琐帝 2024-08-11 07:46:38

有两种不同的方式执行 exe 文件中的代码:

  • 作为单独的进程:使用 System.Diagnostics.Process 并将任何信息作为命令行参数传递
  • 在同一进程中,使用普通的 .NET方法调用
  • 在单独的 AppDomain 中的同一进程中,

这两种方法都可以正常工作;在第一个版本中,您根本不需要主项目来引用 exe:只需将其包含在您的安装/部署项目中(如果有的话)。如果您使用 xcopy 部署(即仅获取输出目录的内容),那么您可以添加对 exe 的引用以复制它。

要使用第二种形式,您需要添加对项目中 exe 文件的引用。如果您愿意在进程中调用代码,这可能是最简单的解决方案。

第三种形式可以使用引用,但不是必须的。这提供了比第二个选项更多的隔离,但比第一个选项更少。

There are two different ways of executing the code in the exe file:

  • As a separate process: use System.Diagnostics.Process and pass in any information as command line arguments
  • In the same process, using normal .NET method calls
  • In the same process in a separate 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. If you're using xcopy deployment (i.e. just taking the contents of the output directory) then you could add a reference to the exe just to get it copied.

To use the second form you'd need to add a reference to the exe file in your project. 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.

稀香 2024-08-11 07:46:38

复制到输出当然是最简单的解决方案。

如果您不想让这个 exe 与您的应用程序文件一起挂在一起,请将其放入资源中,当您的应用程序需要运行该 exe 时,只需将其保存到临时文件夹并从那里运行即可。

这是概念验证代码:

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();
    }
}

在项目属性中,转到资源并将您的 exe 文件拖放到那里。此示例使用名为QueryExpress.exe的应用程序。

您可能还希望将您的应用程序压缩为 ZIP 文件以使其更小。这将在运行该过程之前添加一个解压存档的步骤。

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.

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. This sample is using application named QueryExpress.exe.

You may also wish to compress your app into ZIP file to make it smaller. This will add one more step to unpack archive before running the process.

风透绣罗衣 2024-08-11 07:46:38

您还可以将其作为 Dll 程序集并从应用程序中调用它,我的意思是在运行时加载程序集(如果存在)。

You can also make it as Dll Assembly and call it from your application, I meant loading the assembly at run time if it's existed.

浅唱ヾ落雨殇 2024-08-11 07:46:38

添加对该 exe 的引用不会有帮助,除非您想在主程序中使用 exe 中的类。我认为你的第一个选择是最好的

Adding a reference to that exe won't help, unless you want to use classes from the exe in you main program. I think your first option is the best

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文