-9

我已经在 1 台 PC 上编写了一个程序,并且非常仔细地进行了测试,一切正常。但是当我把我的 .exe 带到另一台计算机上时,它没有我发现的问题是它记住了我的第一台计算机的路径,这很奇怪,因为我从来没有硬编码过任何东西。

该程序所做的是触发一个使用 Process.start() 运行 java 程序的 cmd;

这是结果:

javac:无效标志:C:\Users\Lan 用法:javac 使用 -help 获取可能选项的列表

C:\Users\Lan Nguyen\Desktop\TestGrader\f1>

如果你能看到 \TestGrader\f1; 这是正确的文件夹,这是我想要它去的地方。所以代码工作正常

但是如果您查看 javac: invalid flag: C:\Users\Lan; 这是我最初编写代码的另一台计算机。

我不知道为什么会这样,有人可以帮我解决它。谢谢你。顺便说一句,这与java无关,我只是想使用c#运行一个cmd,稍后将运行java。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace Gui
{
    public partial class GradingProgram : Form
    {
        private String[] studentName;
        private int index = 0;
        String projectName, cmd, compileJava, runJava, path, unzip;

        public GradingProgram()
        {
            studentName = new String[50];
            InitializeComponent();
            cmd = "cmd";
            unzip = "/K rar x ";
            compileJava = "/K javac ";
            button3.Enabled = false;
            this.AutoSize = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderChooser = new FolderBrowserDialog();
            folderChooser.ShowDialog();
            path = folderChooser.SelectedPath;            
        }

        private void updateLabels()
        {
            textBox2.Clear();
            for (int i = 0; i < index; i++)
            {
                textBox2.AppendText(studentName[i]);
                textBox2.AppendText(Environment.NewLine);
            } 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String currentDirectory;

            for (int i = 0; i < index; i++)
            {
                currentDirectory = @path + "\\" + studentName[i];
                Directory.SetCurrentDirectory(currentDirectory);
                foreach (String files in Directory.GetFiles(currentDirectory))
                {
                    if (Path.GetExtension(files) == ".java")
                    {
                        Process.Start(cmd, compileJava + files).WaitForExit();
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            projectName = textBox1.Text;
            runJava = "/K java " + projectName;
            String currentDirectory;
            for (int i = 0; i < index; i++)
            {
                currentDirectory = @path + "\\" + studentName[i];
                Directory.SetCurrentDirectory(currentDirectory);
                textBox3.ScrollBars = ScrollBars.Vertical;
                foreach (String files in Directory.GetFiles(currentDirectory))
                {
                    if (Path.GetExtension(files) == ".java")
                    textBox3.Text += File.ReadAllText(files);
                    textBox3.AppendText("------------------------------------------------------------");
                }
                Process.Start(cmd, runJava).WaitForExit();
                try
                {
                    textBox4.Text = File.ReadAllText(Directory.GetCurrentDirectory() + "\\" + "output.txt");
                }
                catch (Exception )
                {
                    textBox4.Text = "no output";
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Directory.SetCurrentDirectory(@path);
            foreach (String files in Directory.GetFiles(path))
            {
                if (Path.GetExtension(files) == ".rar")
                {
                    Process.Start(cmd, unzip + files).WaitForExit();
                }
            }

            foreach (String folder in Directory.GetDirectories(path))
            {
                studentName[index] = new DirectoryInfo(folder).Name;
                index++;
            }
            button4.Enabled = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            updateLabels();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.Text != "")
            {
                button3.Enabled = true;
            }
        }

    }
}
4

1 回答 1

1

你不应该使用这样的东西:

runJava = String.Format(@"/K java ""{0}""", projectName);

因为您的用户名中的空格似乎失败了。如果您在两台计算机上都有同名的帐户,那么您还有两个 C:\Users\... 目录。

于 2012-09-28T14:03:19.387 回答