簡體   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