在 .NET 中,如何将多个参数发送到 DOS 命令提示符中?
我正在尝试在 ASP.NET 2.0 中执行 DOS 命令。我现在所拥有的称为 BAT 文件,而该文件又称为 CMD 文件。它可以工作(最终结果是文件被 ftp 传输)。但是,我想转储 BAT 和 CMD 文件并在 .NET 中运行所有内容。将多个参数发送到命令窗口的格式是什么?这是我现在所拥有的。
.NET 代码:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "C:\\MyBat.BAT";
proc.Start();
proc.WaitForExit();
Bat 文件如下所示(它所做的只是运行 cmd 文件):
ftp.exe -s:C:\MyCMD.cmd
以下是 Cmd 文件的内容:
open <my host>
<my user name>
<my pw>
quote site cyl pri=1 sec=1 lrecl=1786 blksize=0 recfm=fb retpd=30
put C:\MyDTLFile.dtl 'MyDTLFile.dtl'
quit
I am trying to execute DOS commands in ASP.NET 2.0. What I have now calls a BAT file which, in turn, calls a CMD file. It works (with the end result being a file gets ftp'ed). However, I'd like to dump the BAT and CMD files and run everything in .NET. What is the format of sending multiple arguments into the command window? Here is what I have now.
The .NET Code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "C:\\MyBat.BAT";
proc.Start();
proc.WaitForExit();
The Bat File looks like this (all it does is run the cmd file):
ftp.exe -s:C:\MyCMD.cmd
And here is the content of the Cmd file:
open <my host>
<my user name>
<my pw>
quote site cyl pri=1 sec=1 lrecl=1786 blksize=0 recfm=fb retpd=30
put C:\MyDTLFile.dtl 'MyDTLFile.dtl'
quit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用 ProcessStartInfo.Arguments 交出你的论点:
但是 - 这不是你想要的,因为你也想替换 cmd 文件。但是,内容不是移交给 ftp.exe 的命令行参数。它更像是一个输入脚本。因此将内容作为参数传递是行不通的。
要使 cmd 文件消失,您必须使用标准输入和输出。例如,请参阅 进程。标准输入。
You use ProcessStartInfo.Arguments to hand over your arguments:
But - that's not what you want, since you want to replace the cmd file, too. However, the content is not a commandline argument which is handed over to ftp.exe. It is rather an input script. So it will not work to pass the content as an argument.
To make even the cmd file disappear you have to use the standard input and output. See for instance Process.StandardInput.
使用Process.StartInfo.Arguments。
Use
Process.StartInfo.Arguments
.如果您要启动新流程,只需设置
ProcessStartInfo.Arguments
属性到包含所有参数的字符串,就像您在命令行中键入它们一样。顺便说一句,由于您似乎正在尝试编写命令行 FTP 客户端脚本,因此您可能对 C# FTP 客户端库,或者可能是此问题的答案之一 其他 StackOverflow 问题。
If you're starting a new process, just set the
ProcessStartInfo.Arguments
property to a string containing all your arguments, just as if you had typed them on the command line.As an aside, since it looks like you're trying to script the command-line FTP client, you might be interested in the C# FTP Client library on CodeProject, or perhaps one of the answers to this other StackOverflow question.