将少量参数从C#传递到批处理文件中

时间:2019-04-30 22:35:12

标签: c# forms batch-file

我有这个批处理文件:

@echo off
cd "C:\Program Files (x86)\Microsoft Dynamics 365 Business Central\130\RoleTailored Client\finsql.exe" Command=ExportToNewSyntax, File=%1.%5.txt, Database=%2, ServerName=%3, Filter=Type=%4; ID=%5
pause

对于某些参数,我为动态填充表单创建了文本字段(但现在我使用字符串变量作为示例)。

我用于设置参数的代码:

private void Start_Click(object sender, EventArgs e)
{
    string myBat = @"C:\NAV.bat";

    string _PathtoSourceObject = @"D:\ConvertToExtentions\CAL\";
    string _DataBase = @"Demo Database NAV (13-0)NA";
    string _ServerName = @"testserver";
    string _TypeObject = "table";
    string _IDObject = "22";

    Process process = new Process();

    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardOutput = true;

    process.StartInfo.Arguments = String.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\" \"{4}\"", _PathtoSourceObject, _DataBase, _ServerName, _TypeObject, _IDObject);
    process.StartInfo.FileName = myBat;

    process.Start();
}

问题是我只有一个空的控制台窗口。

你能解释我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

您不能直接运行exe文件吗? 完成后,但是使用完全限定的可执行文件名:

process.StartInfo.Arguments = String.Format("Command=ExportToNewSyntax File=\"{0}\" Database=\"{1}\" ServerName=\"{2}\" Filter=\"{3}\" Type=\"{4}\"", _PathtoSourceObject, _DataBase, _ServerName, _TypeObject, _IDObject);
process.StartInfo.FileName = exeFile; // full qualified name here

在开始该过程之后,添加以下行,以便获得输出和最终错误:

string output = process.StandardOutput.ReadToEnd();
string errors = process.StandardError.ReadToEnd();