Cmd的参数不起作用

时间:2012-07-17 14:43:32

标签: c# visual-studio-2010

从我之前的问题开始,我正在编写一个程序,通过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;

在文件名包含空格的情况下,在整个文件名周围提供引号。

我的问题是我的程序打开CMD Put没有传递任何参数。我检查了filnames(列表)是否正常工作并且它们没问题。看看这些例子,这是做到这一点的方法,但显然是是错的

1 个答案:

答案 0 :(得分: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();