繁体   English   中英

使用VB Project中的命令行参数执行C#控制台exe

[英]Execute the C# console exe with command line arguments from VB Project

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

例如,假设C#exe名称为“ SendEmail.exe ”,其4个参数为From,TO,Subject和Message ,如果我已将exe放在C驱动器中。 这是我从命令提示符下调用的方式

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等)。 这个问题看似愚蠢,但我试图将复杂的问题分成一系列较小的问题并加以解决。

由于您的问题带有C#标记,因此我建议您使用自己喜欢的语言重新编写C#解决方案。

    /// <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
        }
    }

可能不需要通过控制台调用它。 如果它是用C#完成的,并且标记为public而不是internal或private,或者它依赖于public类型,则可以将其添加为VB.Net解决方案中的引用,然后直接调用所需的方法。

这样就好多了,因为您不必担心主题或主体参数中的转义空格或引号之类的事情。

如果您可以控制SendMail程序,则只需进行一些简单的更改就可以访问它。 默认情况下,C#控制台项目为您提供以下内容:

using ....
// several using blocks at the top

// class name
class Program
{
    //static Main() method
    static void Main(string[] args)
    {
        //...
    }
}

您可以像这样从VB.Net使其可用:

using ....
// several using blocks at the top

//Make sure an explicit namespace is declared
namespace Foo
{
    // make the class public
    public class Program
    {
        //make the method public
        static void Main(string[] args)
        {
            //...
        }
    }
}

而已。 同样,只需将引用添加到项目中,将其Import VB.Net文件的顶部,就可以直接调用Main()方法,而无需通过控制台。 没关系,它是.exe而不是.dll。 在.Net世界中,它们都是可以使用的程序集。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM