【问题标题】:adding arguments to process not working?向进程添加参数不起作用?
【发布时间】:2012-08-17 18:46:25
【问题描述】:

我用 C# WinForms 编写了这个程序。

我正在使用 system.diagnostic 创建一个 CMD 进程。 使用该 cmd 我想要一些参数,但它们不存在或不工作:S 不知道为什么?!

注意:我不确定如何使用超过 1 个参数,如果我错了,请纠正我:D 我正在尝试复制“copy /b %filename% lpt1”命令....

这是我的代码:

    public void OutputBtn_Process_Click(object sender, EventArgs e)
    {
        foreach (FileInfo fi in listBox1.Items)
        {
            Process process1 = new Process();
            process1.StartInfo.UseShellExecute = false;
            process1.StartInfo.Arguments = "copy /b myfile.txt test.txt";
            //process1.StartInfo.LoadUserProfile = true;
            process1.StartInfo.FileName = "cmd.exe";
            process1.StartInfo.WorkingDirectory = Path.GetDirectoryName(fi.FullName);
            process1.Start();
        }
    }

【问题讨论】:

  • 注意:我只是添加了 test.txt 而不是 LPT1 用于测试目的。
  • 没错,你只需要“/c”作为第一个参数。
  • 您实际上是否从 listbox1.Items 检索了 FileInfo 实例?
  • 是的。到目前为止它可以工作:) 没有 path.GetdirectoryName 它不会工作,比如“路径无效”
  • 但这也为我的下一个问题打开了大门。如何将我的文件名添加到 cmd 行??

标签: c# process arguments


【解决方案1】:
string strCmdText;
strCmdText= "/C copy /b myfile.txt test.txt";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);

【讨论】:

    【解决方案2】:

    试试这个

     process1.StartInfo.Arguments = "/C \"copy /b myfile.txt LPT1:\""; 
    

    The documentation on Windows 7 command-line tool cmd.exe

    【讨论】:

    • 谢谢。它现在确实做了一些事情。 CMD 窗口快速弹出并再次关闭,说 LPT1 不是识别设备。但这可能是因为我的笔记本电脑上没有 LPT 端口。
    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 2023-02-15
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    相关资源
    最近更新 更多