本文介绍了使用来自 VB 项目的命令行参数执行 C# 控制台 exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调用用 C# 编写的 exe,它接受来自 VB.NET 应用程序的命令行参数.

How to call an exe written in C# which accepts command line arguments from VB.NET application.

例如,让我们假设 C# exe 名称是SendEmail.exe",它的 4 个参数是 From ,TO ,Subject 和 Message,如果我已经放置了exe在C盘.这就是我从命令提示符调用的方式

For Example, Let's assume the C# exe name is "SendEmail.exe" and its 4 arguments are From ,TO ,Subject and Message and If I have placed the exe in the C drive. This is how I call from the command prompt

C:\SendEmail from@email.com,to@email.com,test subject, "Email Message " & vbTab & vbTab & "After two tabs" & vbCrLf & "I am next line"

我想从 VB.NET 应用程序中调用这个SendEmail"exe 并从 VB 传递命令行参数(参数将使用 vb 语法,如 vbCrLf、VBTab 等).这个问题可能看起来很傻,但我试图将复杂的问题分成一系列较小的问题并克服它.

I would like to call this "SendEmail" exe from the VB.NET application and pass the command line arguments from VB (arguments will be using vb Syntax like vbCrLf, VBTab etc). This problem may look silly but I am trying to divide the complex problem into series of smaller issues and conquer it.

推荐答案

因为你的问题有 C# 标签,我会建议一个 C# 解决方案,你可以用你喜欢的语言重新设计.

Because your question has the C# tag, I'll suggest a C# solution you can re-spin in your preferred language.

    /// <summary>
    /// This will run the EXE for the user. If arguments are passed, then arguments will be used.
    /// </summary>
    /// <param name="incomingShortcutItem"></param>
    /// <param name="xtraArguments"></param>
    public static void RunEXE(string incomingExePath, List<string> xtraArguments = null)
    {
        if (File.Exists(incomingExePath))
        {
            System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            if (xtraArguments != null)
            {
                info.Arguments = " " + string.Join(" ", xtraArguments);
            }
            info.WorkingDirectory = System.IO.Path.GetDirectoryName(incomingExePath);
            info.FileName = incomingExePath;
            proc.StartInfo = info;
            proc.Start();
        }
        else
        {
            //do your else thing here
        }
    }

这篇关于使用来自 VB 项目的命令行参数执行 C# 控制台 exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 02:55