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

标签 c# command-line

我编写了一个程序,使用 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 然后是 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);
    }
}

关于c# - 命令行中带有空格的预期行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17877544/

相关文章:

ruby - 脚本错误消息: "Could not find command in namespace"

command-line - 找不到 NGINX brew install 命令

c# - Amazon EC2 开发堆栈

c# - 特定类型的 List<T> 导致 ComboBox 中断

c# - 不想等待方法返回

linux - 将文件从一个目录移动到另一个目录并添加到新目录中的每个文件名

unix - 在 unix 中切换到父目录

c# - 当我们在设计器中指定列时,如何填充gridview?

c# - 从包中检索当前应用程序版本

macos - 将星号传递给命令行程序会使文件名出现在 argv 中