-2

从我之前的问题来看,我正在编写一个通过 CMD 执行多个文件的程序。

这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace Convert
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    private void BtnSelect_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog Open = new OpenFileDialog();
        Open.Filter = "RIFF/RIFX (*.Wav)|*.wav";
        Open.CheckFileExists = true;
        Open.Multiselect = true;
        Open.ShowDialog();
        LstFile.Items.Clear();
        foreach (string file in Open.FileNames)
        {
            LstFile.Items.Add(file);
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        LstFile.Items.Clear();
    }

    private void BtnConvert_Click(object sender, RoutedEventArgs e)
    {       Process p = new Process();      
            p.StartInfo.FileName = "cmd";
            p.StartInfo.UseShellExecute = false;
            foreach (string fn in LstFile.Items)
            {
                string fil = "\"";
                string gn = fil + fn + fil;
                p.Start();
                p.StartInfo.Arguments = gn;
            }            
        }      
    }    
}

我用了

string fil = "\"";
string gn = fil + fn + fil;

如果文件名有空格,请在完整文件名周围提供引号。

我的问题是我的程序 Opens CMD Put 没有在其中传递任何参数。我检查了 filnames(list) 是否正常工作并且它们很好。查看示例这是执行此操作的方法,但显然有问题

4

1 回答 1

2

StartInfo.Arguements 

在您开始流程之前,我建议为您启动的每个流程创建一个新的流程类。

例子:

        foreach (string fn in LstFile.Items)
        {
            string fil = "\"";
            string gn = fil + fn + fil;

            Process p = new Process();
            p.StartInfo.FileName = "cmd";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.Arguments = gn;
            //You can do other stuff with p.StartInfo such as redirecting the output
            p.Start();
            // i'd suggest adding p to a list or calling p.WaitForExit();, 
            //depending on your needs.  
        }

如果您尝试调用 cmd 命令,请提出您的论点

"/c \"what i would type into the command Line\""

这是我快速完成的一个例子。它在记事本中打开一个文本文档

        Process p = new Process();
        p.StartInfo.FileName = "cmd";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.Arguments = "/c \"New Text Document.txt\"";
        p.Start();
        p.WaitForExit();
于 2012-07-17T14:50:18.970 回答