运行批处理文件

时间:2014-04-14 19:31:11

标签: c# batch-file

我一直在为一个Minecraft服务器的经理应用程序工作,当我运行我的程序时,控制台显示并消失,如果我手动运行它,它运行没有和问题。我的代码:

Process process = new Process
            {
                StartInfo =
                {
                    FileName = textBox2.Text,
                    //Arguments = textBox3.Text,
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = false,
                }
            };

process.OutputDataReceived += new DataReceivedEventHandler(server_outputDataReceived);
server = process;
process.Start();

批处理文件代码(idk是什么语言的批处理文件,所以我使用默认语言 - 选择语言):

java -Xmx1024M -jar craftbukkit-1.7.2-R0.3.jar -o false

顺便说一句。你能在不创建文件的情况下启动进程(例如,启动进程“java -jar示例”,不创建文件)?

@Edit 回答第三个问题:Answer to the third question

我的完整代码(MessageBoxes是波兰语,因为我来自波兰,但后来我会添加对其他语言的支持):

using System;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public Process server;

        private Boolean runServer()
        {
            if (!File.Exists(textBox2.Text))
            {
                MessageBox.Show("Brak określonej ścieżki dostępu! (" + textBox2.Text + ")", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            Process process = new Process
            {
                StartInfo =
                {
                    FileName = textBox2.Text,
                    //Arguments = textBox3.Text,
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow = false,
                }
            };

            process.OutputDataReceived += new DataReceivedEventHandler(server_outputDataReceived);
            process.ErrorDataReceived += new DataReceivedEventHandler(server_outputDataReceived);
            server = process;

            if (process.Start())
                return true;
            else
            {
                MessageBox.Show("Nie można włączyć serwera!", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private String ReadFile(String filename, int line)
        {
            StreamReader reader = new StreamReader(filename);

            for (int i = 0; i < line; i++)
            {
                reader.ReadLine();
            }

            return reader.ReadLine();
        }

        private void ReloadOPs()
        {
            if (!File.Exists(textBox1.Text))
            {
                MessageBox.Show("Sciezka dostępu do pliku z listą graczy posiadających OP nie istnieje! (" + textBox1.Text + ")", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                tabControl1.SelectedTab = tabPageOptions;
                textBox1.SelectAll();
                return;
            }

            String line = ReadFile(textBox1.Text, 0);
            comboBox1.Items.Clear();
            for (int i = 1; i < File.ReadAllLines(textBox1.Text).Length; i++)
            {
                if (!String.IsNullOrWhiteSpace(ReadFile(textBox1.Text, i)))
                {
                    comboBox1.Items.Add(line);
                    line = ReadFile(textBox1.Text, i);
                }
            }

            MessageBox.Show("Lista graczy z OP, została odświeżona.");
        }

        // OPs combobox (OPs)
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            groupBox1.Text = comboBox1.SelectedItem.ToString();
            groupBox1.Visible = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = Application.StartupPath.ToString() + @"\ops.txt";
            ReloadOPs();
        }

        // Reload OPs button (OPs)
        private void button1_Click(object sender, EventArgs e)
        {
            ReloadOPs();
        }


        // Save button (Options)
        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void server_outputDataReceived(object sender, DataReceivedEventArgs e)
        {
            addConsoleMessage(e.Data.ToString(), true);
        }

        // Run server button (Menu)
        private void button5_Click(object sender, EventArgs e)
        {
            if (!runServer())
                return;

            server.BeginOutputReadLine();
            button6.Enabled = true;
        }

        // Stop server button (Menu)
        private void button6_Click(object sender, EventArgs e)
        {
            if(!server.HasExited)
                server.Kill();
            button6.Enabled = false;
        }

        private void addConsoleMessage(String message, Boolean refresh)
        {
            listBox1.Items.Add(message);
            if (refresh)
                listBox1.Refresh();
        }
    }
}

现在可以使用,但是在第二条消息后批处理文件返回(?)之后,程序崩溃,因为InvaildOperationException未处理。

1 个答案:

答案 0 :(得分:0)

确定。让我们澄清一些观点。我假设您有一个名为filename.bat的批处理文件,其中包含以下内容:

java -Xmx1024M -jar craftbukkit-1.7.2-R0.3.jar -o false

并使用&#34;手动运行&#34;您的意思是打开命令行窗口并输入:filename。好?但是,您没有说明如何在不手动运行批处理文件时运行批处理文件!如果从文件浏览器中双击它,则缺少批处理文件以将当前目录更改为批处理文件所在的目录:

cd "%~P0"
java -Xmx1024M -jar craftbukkit-1.7.2-R0.3.jar -o false

PS - 您的批处理文件不应命名为start.bat,因为start是内部cmd.exe命令的名称!