简体   繁体   English

使用批处理文件从命令行调用C#exe

[英]calling C# exe from commandline using batch file

I want to create an exe file using C# which will send emails. 我想使用C#创建一个exe文件,它将发送电子邮件。 I want to invoke this exe using batch file. 我想使用批处理文件调用此exe。 Should I create a Web form application or just a class library ? 我应该创建一个Web表单应用程序还是只创建一个类库? Can an exe be called using batch file from command prompt? 可以从命令提示符使用批处理文件调用exe吗?

Please suggest. 请建议。

Typically, if you want to call a application from a command line, you'd create a Console Application . 通常,如果要从命令行调用应用程序,则需要创建控制台应用程序

This allows your command prompt to call the application and (optionally) accept input from the console (prompt) as well as write output to the console (via the Console class ). 这允许您的命令提示符调用应用程序并(可选)接受来自控制台的输入(提示)以及将输出写入控制台(通过Console类 )。

You can read the command line arguments directly from the Main routine in the Console Application, as well. 您也可以直接从Console Application中的Main例程中读取命令行参数。

Write a console app that takes arguments on the command line. 编写一个在命令行上接受参数的控制台应用程序。 You can then call this from script (ie a batch file). 然后,您可以从脚本(即批处理文件)中调用它。 The arguments that you pass could contain all the elements of your email - the body may be a little large and I am not sure of the limit (if there is one) for the length of string args, but as long as the body is reasonable in size you will be ok. 您传递的参数可能包含电子邮件的所有元素 - 正文可能有点大,我不确定字符串参数的长度(如果有的话),但只要正文是合理的在大小你会没事的。

I prefer writing "tools" such like this as console apps (over Windows apps) because you have so much flexibility in terms of executing the app. 我更喜欢编写像这样的“工具”作为控制台应用程序(通过Windows应用程序),因为您在执行应用程序方面具有很大的灵活性。 You can invoke it directly by typing in the cmd.exe (the command line), you can write a script to call it (as you have suggested), you can write another .NET application that can call it, etc... A Windows app has just one method of invocation. 您可以通过键入cmd.exe(命令行)直接调用它,您可以编写一个脚本来调用它(如您所建议的那样),您可以编写另一个可以调用它的.NET应用程序,等等... A Windows应用程序只有一种调用方法。

Of course. 当然。 Create the application, and store the binary exe in some folder (ie, C:\\folder). 创建应用程序,并将二进制exe存储在某个文件夹(即C:\\文件夹)中。
If your exe has the name "name.exe", in your batch file, write 如果您的exe名称为“name.exe”,请在批处理文件中写入

cd C:\folder
name

This works for all types of applications, and you can access command-line data in both, but it's easiest in a console application. 这适用于所有类型的应用程序,您可以在两者中访问命令行数据,但它在控制台应用程序中最简单。

You would write: 你会写:

cd C:\Users\asif\Desktop\EmailSender\ConsoleApplication1\bin\Debug\
::Space here
ConsoleApplicati‌​on1

with the new line . 用新线

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

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