命令行中带有空格的预期行为

本文关键字:空格 命令行 | 更新日期: 2023-09-27 18:25:05

我编写了一个程序,使用csc.exe:编译我的.cs文件

namespace myCompiler
{
    public partial class Form1 : Form
    {
        string compilerFolder;
        string outputFolder;
        string projectFile;
        string output = @" /out:";
        public Form1()
        {
            InitializeComponent();
        }
        private void startCompile_Click(object sender, EventArgs e)
        {
            Compile();
        }
        public void findCompile_Click(object sender, EventArgs e)
        {
            DialogResult result1 = folderBrowserDialog1.ShowDialog();
            compilerFolder = folderBrowserDialog1.SelectedPath;
            MessageBox.Show(compilerFolder);
            cscLabel.Text = compilerFolder;
        }
        private void outputCompile_Click(object sender, EventArgs e)
        {
            DialogResult result2 = folderBrowserDialog2.ShowDialog();
            outputFolder = folderBrowserDialog2.SelectedPath;
            outputLabel.Text = (outputFolder);
            MessageBox.Show(outputFolder);
        }
        private void findProject_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                projectFile = openFileDialog1.FileName;                
                projectLabel.Text = (projectFile);
                MessageBox.Show(projectFile);
            }
        }
        public void Compile()
        {
            try
            {
                Process compile = new Process();
                string outputExe = fileName.Text;
                string compiler = compilerFolder + @"'csc.exe";
                string arGs = output + outputFolder + @"'" + outputExe + " " + projectFile;
                compile.StartInfo.FileName = (compiler);
                compile.StartInfo.Arguments = (arGs);
                compile.StartInfo.RedirectStandardOutput = true;
                compile.StartInfo.UseShellExecute = false;
                compile.Start();
                string stdOutput = "";
                while (!compile.HasExited)
                {
                    stdOutput += compile.StandardOutput.ReadToEnd();
                    MessageBox.Show(stdOutput);
                }
            }
            catch (Exception errorMsg)
            {
                MessageBox.Show(errorMsg.Message);
            }
        }
        private void testButton_Click(object sender, EventArgs e)
        {
            MessageBox.Show(projectFile);
            MessageBox.Show(compilerFolder);
        }   
    }
}

编译指令运行,但没有产生任何结果,只是控制台黑色屏幕的快速闪烁。

基本上,似乎正在发生的事情是,当所有字符串都在命令行中被解析为进程的参数时,.cs项目源目录被每个空白区分解,即c:'users'%username%'Documents'Visual Studio 2010'被分解为c:'users'%username%'Documents'Visual then Studio,然后2010'Projects'Myproject'myproj.cs

并且随后编译失败。

命令行中带有空格的预期行为

您需要在带有空格的文件路径周围使用双引号。

请参阅下面我对您代码的编辑。

public void Compile()
{
    try
    {
        Process compile = new Process();
        string outputExe = fileName.Text;
        string compiler = compilerFolder + "'csc.exe";
        // add double quotes around path with spaces in it.
        string arGs = output + "'"" + outputFolder + "'"" + @"'" + outputExe + " " + projectFile; 
        compile.StartInfo.FileName = compiler;
        compile.StartInfo.Arguments = arGs;
        compile.StartInfo.RedirectStandardOutput = true;
        compile.StartInfo.UseShellExecute = false;
        compile.Start();
        string stdOutput = "";
        while (!compile.HasExited)
        {
            stdOutput += compile.StandardOutput.ReadToEnd();
            MessageBox.Show(stdOutput);
        }
    }
    catch (Exception errorMsg)
    {
        MessageBox.Show(errorMsg.Message);
    }
}