使用VB项目中的命令行参数执行C#console exe
如何调用用C#编写的exe,它接受VB.NET应用程序中的命令行参数 例如,假设C#exe名称为“sendmail.exe”,它的4个参数是From、TO、Subject和Message,如果我已将exe放在C驱动器中。这是我在命令提示符下调用的方式使用VB项目中的命令行参数执行C#console exe,c#,.net,vb.net,C#,.net,Vb.net,如何调用用C#编写的exe,它接受VB.NET应用程序中的命令行参数 例如,假设C#exe名称为“sendmail.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" &
C:\SendEmail from@email.com,to@email.com,test subject, "Email Message " & vbTab & vbTab & "After two tabs" & vbCrLf & "I am next line"
我想从VB.NET应用程序调用这个“sendmail”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
}
}
//
///这将为用户运行EXE。如果传递了参数,则将使用参数。
///
///
///
公共静态void RunEXE(字符串incomingExePath,列表xtraArguments=null)
{
if(File.Exists(incomingExePath))
{
System.Diagnostics.ProcessStartInfo info=新的System.Diagnostics.ProcessStartInfo();
System.Diagnostics.Process proc=新的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();
}
其他的
{
//在这里做你的其他事情
}
}
由于您的问题带有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
}
}
//
///这将为用户运行EXE。如果传递了参数,则将使用参数。
///
///
///
公共静态void RunEXE(字符串incomingExePath,列表xtraArguments=null)
{
if(File.Exists(incomingExePath))
{
System.Diagnostics.ProcessStartInfo info=新的System.Diagnostics.ProcessStartInfo();
System.Diagnostics.Process proc=新的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();
}
其他的
{
//在这里做你的其他事情
}
}
您可能不需要通过控制台调用它。如果它是在C#中完成的,并且标记为public而不是internal或private,或者如果它依赖于公共类型,则可以将其添加为VB.Net解决方案中的引用,并直接调用所需的方法
这是如此干净和更好,因为你不必担心像转义空格或引号在你的主题或身体的论点
如果您可以控制SendMail程序,只需进行一些简单的更改即可访问它。默认情况下,C#控制台项目会提供如下内容:
using ....
// several using blocks at the top
// class name
class Program
{
//static Main() method
static void Main(string[] args)
{
//...
}
}
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)
{
//...
}
}
}
您可以通过VB.Net使其可用,如下所示:
using ....
// several using blocks at the top
// class name
class Program
{
//static Main() method
static void Main(string[] args)
{
//...
}
}
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
it位于VB.Net文件的顶部,就可以直接调用Main()方法,而无需通过控制台。没关系,它是一个.exe而不是.dll。在.Net世界中,它们都只是您可以使用的程序集。您可能不需要通过控制台调用它。如果它是在C#中完成的,并且标记为public而不是internal或private,或者如果它依赖于公共类型,则可以将其添加为VB.Net解决方案中的引用,并直接调用所需的方法
这是如此干净和更好,因为你不必担心像转义空格或引号在你的主题或身体的论点
如果您可以控制SendMail程序,只需进行一些简单的更改即可访问它。默认情况下,C#控制台项目会提供如下内容:
using ....
// several using blocks at the top
// class name
class Program
{
//static Main() method
static void Main(string[] args)
{
//...
}
}
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)
{
//...
}
}
}
您可以通过VB.Net使其可用,如下所示:
using ....
// several using blocks at the top
// class name
class Program
{
//static Main() method
static void Main(string[] args)
{
//...
}
}
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
it位于VB.Net文件的顶部,就可以直接调用Main()方法,而无需通过控制台。没关系,它是一个.exe而不是.dll。在.Net世界中,它们都只是您可以使用的程序集。是控制台应用程序还是要从命令行运行进程?我在这里找到了解决方案是控制台应用程序还是要从命令行运行进程?我在这里找到了解决方案