0

我从未编写过应用程序,因此我将发布整个代码(80 行)。(我来自编写脚本的背景。)

我的目标是加载或创建“已使用”端口列表,选择不在列表中的范围内的数字,如果尝试到达未使用端口的次数达到 129,则运行 Windows 批处理文件.

这也会将所选端口转换为 .cmd

(其中一些是来自 SO 的来源的合并)

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.IO;
namespace randomport
{
    class Core
    {
        public const int 
            minval = 8001,
            maxval = 8128;
        public static int[] usedPorts = new int[]{};
        public static int 
            chosenPort = 0,
            tries = 0,
            timeout = 10;
        public static bool read = false;
        public static void Main()
        {
            if(!read)
            {
                Read();
                read = true;
            }
            RandGen();
        }
        public static void RandGen()
        {
            Process proc = null;
            Random rand = new Random();
            if(tries < 129) chosenPort = rand.Next(minval, maxval);
            else
            {
                proc.StartInfo.FileName                 = @"C:\Users\Administrator\Desktop\TerrariaServer\filebin\sendservfull.bat";
                proc.StartInfo.RedirectStandardError    = true;
                proc.StartInfo.RedirectStandardOutput   = true;
                proc.StartInfo.UseShellExecute          = false;

                proc.Start();

                proc.WaitForExit
                    (
                        (timeout <= 0)
                        ? int.MaxValue : timeout * 100 * 60
                    );
            }
            for(int i = 0; i < usedPorts.Length; i++)
            {
                if(chosenPort != usedPorts[i])
                {
                    Write();
                //  Application.Exit();
                }
                else
                {
                    tries += 1;
                    return;
                }
            }
        }
        public static void Read()
        {
            using (StreamReader sr = new StreamReader(@"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt")) 
            {
                string[] values = sr.ReadToEnd().Split(';');
                for(int i = 0; i < values.Length; i++)
                {
                    int.TryParse(values[i], out usedPorts[i]);
                }
            }
        }
        public static void Write()
        {
            File.AppendAllText(@"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", "set port="+chosenPort+";");
            File.Move(@"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", Path.ChangeExtension(@"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", ".cmd"));
        }
    }
}

我在最终导出方面还有一些工作要做(比如删除“;”)。

该脚本编译,但未按预期运行。肯定有问题,但我不知道。如果是明显的东西,我想那会很方便,否则如果只是格式等,我显然需要做更多的研究。

4

1 回答 1

0

这次使用visual studio 2008 express编译,并清理干净。如果没有调试器,很难判断哪些是问题,例如缺少括号。

文件写入,编译和崩溃解决了..

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.IO;
namespace randomport
{
    class Core
    {
        public static int[] usedPorts = new int[] { };
        public static int
            minval = 8001,
            maxval = 8129,
            chosenPort = 0,
            timeout = 10,
            temp = 1024;
        public static bool read = false;
        public static void Main(string[] args)
        {
            string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
            if (!Directory.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
            {
                Directory.CreateDirectory(String.Concat(path, "\\desktop\\TerrariaServer\\filebin"));
                //  using (StreamWriter sw = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"))) { }
            }
            if (!Directory.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd")))
            {
                Directory.CreateDirectory(String.Concat(path, "\\desktop\\TerrariaServer\\filebin"));
                using (StreamWriter sw = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd"))) { }
            }
            if (args.Length > 0)
            {
                if (args[0] == "-noread")
                {
                }
                else if (args[0] == "-read" || args[0] == "-default")
                {
                    if (!read)
                    {
                        Read();
                        read = true;
                    }
                }
            }
            else
            {
                if (!read)
                {
                    Read();
                    read = true;
                }
            }
            if (args.Length >= 3)
            {
                if (args[1] != "-default" || args[1] != "0")
                {
                    int.TryParse(args[1], out temp);
                    if (temp > 1024 && temp < 65535)
                    {
                        minval = temp;
                    }
                }
                if (args[2] != "-default" || args[2] != "0")
                {
                    int.TryParse(args[2], out temp);
                    if (temp > 1024 && temp < 65535)
                    {
                        maxval = temp;
                    }
                }
            }
            RandGen();
        }
        public static void RandGen()
        {
            string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
            Random rand = new Random();
            chosenPort = rand.Next(minval, maxval);
            for (int i = 0; i < usedPorts.Length; i++)
            {
                if (chosenPort != usedPorts[i])
                {
                    Write();
                }
                else return;
            }
        }
        public static void Read()
        {
            string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
            if (!File.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
            {
                File.Create(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"));
            }
            using (StreamReader sr = new StreamReader(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
            {
                string[] values = sr.ReadToEnd().Split(';');
                usedPorts = new int[values.Length];//Initialize the array with the same length as values
                for (int i = 0; i < values.Length; i++)
                {
                    int.TryParse(values[i], out usedPorts[i]);
                }
            }
        }
        public static void Write()
        {
            string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
            File.AppendAllLines(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"), new string[] { chosenPort + ";" });
            using (StreamWriter sw2 = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd")))
            {
                sw2.WriteLine("set port=" + chosenPort);
            }
            Environment.Exit(0);
        }
    }
}
于 2013-10-05T08:11:30.400 回答