使用 Process 执行带有参数的批处理
我正在尝试使用 Process 类从 C# 代码执行批处理文件。批量获取参数,作为带引号的字符串传递到 ProcessStartInfo.Arguments 中。一切都工作得很好,除了当执行批处理时它前面带有一些垃圾 ascii 符号这一事实之外,我无法弄清楚它们来自哪里。 批处理脚本看起来很简单:
%1 -S .\sqlexpress -i %2
批处理只是在特定位置执行sqlcmd.exe,并传入要执行的SQL脚本。当我在命令行中手动尝试此操作时,效果很好,因此这不是生成的命令本身。有想法吗?
构建脚本执行结果如下所示: ![
[gallio] D:\Development\project_A\Trunk\build\compile>"D:\Development\project_A\Trunk\build\tools\sqlcmd\SQLCMD.EXE" -S .\sqlexpress -i "C:\Temp\project_A_consolidated_sql_scripts.sql"
[gallio] '' is not recognized as an internal or external command,
[gallio] operable program or batch file.
[gallio] Creating project_A database
[gallio] Changed database context to 'master'.
[gallio] Creating project_ADBUser login
[gallio] Changed database context to 'project_A'.
[gallio] Creating project_ADBUser user
[gallio] Creating project_A Schema
[gallio] Changed database context to 'project_A'.
]图片
谢谢。
I am trying to execute a batch file from C# code using Process class. Batch gets to parameters, passed as quoted strings into ProcessStartInfo.Arguments. Everything is working great, besides the fact that when batch is executed its pre-pended with a few garbage ascii symbols I cannot figure out where they coming from.
Batch script looks simple:
%1 -S .\sqlexpress -i %2
Batch is just executing sqlcmd.exe at a specific location and passes in SQL script to be executed. When I try this manually in command line, it works great, so this is not the generated command itself. Ideas?
This is what build script execution result looks like:
![
[gallio] D:\Development\project_A\Trunk\build\compile>"D:\Development\project_A\Trunk\build\tools\sqlcmd\SQLCMD.EXE" -S .\sqlexpress -i "C:\Temp\project_A_consolidated_sql_scripts.sql"
[gallio] '' is not recognized as an internal or external command,
[gallio] operable program or batch file.
[gallio] Creating project_A database
[gallio] Changed database context to 'master'.
[gallio] Creating project_ADBUser login
[gallio] Changed database context to 'project_A'.
[gallio] Creating project_ADBUser user
[gallio] Creating project_A Schema
[gallio] Changed database context to 'project_A'.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您那里有一个 UTF-8 BOM(字节顺序标记)。但很难说这是在批处理文件本身还是命令行参数中。你从哪里获取数据?
My guess is that you've got a UTF-8 BOM (byte order mark) there. Whether that's in the batch file itself or the command line argument is hard to say though. Where are you getting the data from?