代码之家  ›  专栏  ›  技术社区  ›  Nhmanas

在发送到cmd的c#命令上使用引号

  •  0
  • Nhmanas  · 技术社区  · 6 年前

    关于这一点,我已经搜索了很多,但没有任何关于这种具体情况的例子。

    我想压缩写在文本框(dirdes)上的目录,并在富文本框上显示输出( _output ).

    它不是压缩写在文本框上的目录,而是压缩bin目录。

    private void button3_Click(object sender, EventArgs e)
    {
        string dirdes1 = dirdes.Text;
        string strCmdText;
        strCmdText = "/C compact /c /s /a /i /exe:lzx '" + dirdes1 + " *'";
        Process lzx = new Process();
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        lzx.StartInfo.FileName = "cmd.exe";
        lzx.StartInfo.UseShellExecute = false;
        lzx.StartInfo.Arguments = strCmdText;
        lzx.StartInfo.RedirectStandardOutput = true;
        lzx.Start();
        _output.Text = lzx.StandardOutput.ReadToEnd();
    }
    

    编辑:我认为这是一个报价错误。因为目录必须是这样的 "C:\" 不是这样的 'C:\'

    1 回复  |  直到 6 年前
        1
  •  0
  •   Nhmanas    6 年前

    我在GUI中混合了文本框名称。。。这花了我24小时! 我对代码做了一些修改。现在它起作用了

    private void button3_Click(object sender, EventArgs e)
        {
            string dirdes1 = dirdes.Text;
            string strCmdText;
            string locationAddress;
            string cdCommand;
            string doCompress;
            locationAddress = dirdes1;
            cdCommand  = "/C " + "cd " + locationAddress;
            strCmdText = "compact /c /s /a /i /exe:lzx";
            doCompress = "/C " + strCmdText + " *";
            Process lzx = new Process();
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            lzx.StartInfo.FileName = "cmd.exe";
            lzx.StartInfo.UseShellExecute = false;
            lzx.StartInfo.WorkingDirectory = @locationAddress;
            lzx.StartInfo.Arguments = doCompress;
            lzx.StartInfo.RedirectStandardOutput = true;
            lzx.Start();
            _output.Text = lzx.StandardOutput.ReadToEnd();
            showCommand.Text = doCompress;
    
        }
    

    有些变量是用来测试的别担心