5

我编写了一个使用 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 Studiothen2010\Projects\Myproject\myproj.cs

随后编译失败。

4

1 回答 1

3

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

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

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);
    }
}
于 2013-07-26T09:26:01.543 回答