使用 Process 执行带有参数的批处理

发布于 2024-08-17 11:33:18 字数 1085 浏览 7 评论 0原文

我正在尝试使用 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'.

]Image

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的軌跡 2024-08-24 11:33:18

我的猜测是,您那里有一个 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文