【发布时间】:2019-04-30 22:35:11
【问题描述】:
我有这个批处理文件:
@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();
}
问题是我得到的只是一个空的控制台窗口。
你能解释一下我错过了什么吗?
【问题讨论】:
-
流式传输错误输出,以便您查看问题所在
StreamReader reader = process.StandardError; Console.WriteLine(reader.ReadLine()); -
批处理文件是文本文件而不是程序,由 CMD.exe 运行。你用
ShellExecute=False说不要使用文件扩展名,也没有理由,因为这依赖于用户如何配置他们的系统。 EG:一些 IT 部门将批处理从默认运行更改为默认编辑。输入cmd /?注意:您必须至少使用/c或/k。 -
你也不能
CD到一个程序。删除cd。输入CD /? -
ServerName=%3, Filter=Type=%4; ID=%5在您的实际batch-file 中是否位于新行?还是您的问题中存在需要修复的错误? -
应该是没有文本输出的控制台窗口。您正在重定向 Stdout 和 Stderr 流。通常,C# 程序会读取重定向的流,并且可以隐藏控制台窗口,因为没有文本的控制台窗口无法显示。
标签: c# forms batch-file