Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 运行批处理文件_C#_Batch File - Fatal编程技术网

C# 运行批处理文件

C# 运行批处理文件,c#,batch-file,C#,Batch File,我一直在为Minecraft服务器开发一个manager应用程序,当我运行我的程序时,控制台会显示并消失,如果我手动运行,它运行时不会出现任何问题。我的代码: Process process = new Process { StartInfo = { FileName = textBox2.Text, //Arguments =

我一直在为Minecraft服务器开发一个manager应用程序,当我运行我的程序时,控制台会显示并消失,如果我手动运行,它运行时不会出现任何问题。我的代码:

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批处理文件是什么语言,所以我使用了默认的one-Select语言:

java -Xmx1024M -jar craftbukkit-1.7.2-R0.3.jar -o false
顺便说一句,你能在不创建文件的情况下启动流程吗?例如,启动流程java-jar示例,而不创建文件

@编辑第三个问题的答案:

我的完整代码消息框使用波兰语,因为我来自波兰,但稍后我将添加对其他语言的支持:

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,程序崩溃。

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

java -Xmx1024M -jar craftbukkit-1.7.2-R0.3.jar -o false
手动运行意味着打开一个命令行窗口并输入:filename。好啊但是,您尚未指明在不手动运行批处理文件时如何运行该文件!如果从文件浏览器双击该文件,则批处理文件将丢失,以将当前目录更改为批处理文件所在的目录:

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

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

嗯?唯一的问题是控制台没有显示任何内容吗?这里没有任何东西会告诉控制台等待输入或显示任何内容。手动运行是什么意思?您是否尝试过调试此代码?可能发生了异常,您正在点击内部Exit0或尝试调试并报告备份。不,控制台刚刚关闭。手动运行-手动运行bat文件,其中是批处理文件代码start.bat。我建议您订阅ErrorDataReceived事件。我会检查它,请稍等@选中编辑,它没有显示任何消息,只是空白。现在可以工作了,但在批处理文件返回的第二条消息之后,由于未处理InvaildOperationException,程序崩溃。