将参数传递到控制台应用程序 [英] Passing arguments to a console application

查看:110
本文介绍了将参数传递到控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下命令成功地将参数传递到控制台应用程序:

  ProcessStartInfo psi = new ProcessStartInfo 
psi.FileName = Properties.Settings.Default.EmailBlasterAppPath;

string subject = txtSubject.Text.Replace(@,@);
string emailbody = txtEmail.Text.Replace(@,@);
string args = String.Format({0},{1},{2},{3},{4},
SendPlainEmail,
B2BSession.Doctor.DoctorId .ToString(),
DocEmail.Trim(),subject,emailbody);

psi.Arguments = args;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

过程process = Process.Start(psi);

但是,一旦在控制台应用程序中,控制台应用程序只需要参数作为单个字符串(见下文):

  static void Main(string [] args)
/ pre>

我的问题是,我将主题行和电子邮件正文传递到控制台应用程序,但它只拾取参数中的第一个单词。所以,如果我传入这样的东西(注意,测试主题行应该是一个参数,测试主体行将是另一个参数)共5个参数:



SendPlainEmail 25498 test@test.com测试主题行测试主体行



但是,控制台应用程序会将其解析为有9个参数(每个单词一个) :



SendPlainEmail 25498 test@test.com测试主题行测试主体行



有办法通过parms进入控制台应用程序,只有5个参数,主体和正文被视为单个参数?



所以,正如你可以看到,就在Process之前。开始语句,包含在psi.arguments属性中。



数据正确传递,只是接收控制台应用程序只能接收一个字符串数组通过空格。因此,代替第四和第五个参数分别是测试主题行和测试主体行,控制台应用程序将其处理如下:



args [3 ] = test



args [4] = subject



args [5] = line



args [6] = test



args [7] = body



args [8] = line



因此,我应该如何处理测试主题行和测试主体行 array

$ p

解决方案

您需要用引号将参数括起来(),因此就像

  SendPlainEmail 25498 test@test.com测试主题行测试主体行

否则它不知道你的主体在哪里结束,你的身体开始;


I am able to successfully pass arguments into a console application using the following:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Properties.Settings.Default.EmailBlasterAppPath;

string subject = txtSubject.Text.Replace(@"""", @"""""");
string emailbody = txtEmail.Text.Replace(@"""", @"""""");
string args = String.Format("{0},{1},{2},{3},{4}",
                            "SendPlainEmail",
                            B2BSession.Doctor.DoctorId.ToString(),
                            DocEmail.Trim(), subject, emailbody);

psi.Arguments = args;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;

Process process = Process.Start(psi);

However, once inside the console application, the console app only expects parameters as single strings separated by a space (see below):

static void Main(string[] args)

My problem is that I'm passing a subject line and email body into the console application, but it only picks up the first word in the parameter. So, if I pass in something like this (note that "test subject line" should be one argument and "test body line" would be another argument) for a total of 5 arguments:

SendPlainEmail 25498 test@test.com test subject line test body line

but, the console app would parse it as having 9 arguments (one for each word):

SendPlainEmail 25498 test@test.com test subject line test body line

Is there a way to pass the parms into the console app with just the 5 arguments where the subject and body are treated as a single argument?

So, as you can see, right before the Process.Start statement, what is contained in the psi.arguments property.

The data is being passed correctly, it's just that the receiving console application can only receive an array of strings separated by a space. So, instead of the 4th and 5th arguments being "test subject line" and "test body line" respectively, the console app process it as the following:

args[3] = test

args[4] = subject

args[5] = line

args[6] = test

args[7] = body

args[8] = line

So, in lieu of this, how can I treat "test subject line" and "test body line" as one item in the array respectively?

解决方案

You need to surround the parameters in quotes ( " ), so it's like

SendPlainEmail 25498 test@test.com "test subject line" "test body line"

else it wouldn't know where your subject ends and your body begins. ;)

这篇关于将参数传递到控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆