C# 端口选择和批处理运行程序应用

C# 端口选择和批处理运行程序应用,c#,random,C#,Random,我从未编写过应用程序,因此我将发布全部代码(80行)。(我有编写脚本的背景。) 我的目标是加载或创建一个“已使用”端口列表,选择一个不在列表范围内的数字,如果尝试访问未使用端口的数量达到129,则运行windows批处理文件 这也会将所选端口转换为.cmd (其中一些是SO来源的合并) 使用系统; 使用System.Collections.Generic; 使用系统文本; 使用System.Linq; 使用系统诊断; 使用System.IO; 命名空间随机端口 { 类核心 { 公共常数 minv

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

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

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

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

使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Linq;
使用系统诊断;
使用System.IO;
命名空间随机端口
{
类核心
{
公共常数
minval=8001,
maxval=8128;
公共静态int[]usedport=newint[]{};
公共静态整数
chosenPort=0,
尝试=0,
超时=10;
公共静态bool read=false;
公共静态void Main()
{
如果(!读取)
{
Read();
读=真;
}
RandGen();
}
公共静态void RandGen()
{
processproc=null;
Random rand=新的Random();
如果(trys<129)chosenPort=rand.Next(minval,maxval);
其他的
{
proc.StartInfo.FileName=@“C:\Users\Administrator\Desktop\terriaserver\filebin\sendservfull.bat”;
proc.StartInfo.RedirectStandardError=true;
proc.StartInfo.RedirectStandardOutput=true;
proc.StartInfo.UseShellExecute=false;
proc.Start();
WaitForExit程序
(

(timeout这一次使用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);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Linq;
使用系统诊断;
使用System.IO;
命名空间随机端口
{
类核心
{
公共静态int[]usedport=newint[]{};
公共静态整数
minval=8001,
maxval=8129,
chosenPort=0,
超时=10,
温度=1024;
公共静态bool read=false;
公共静态void Main(字符串[]args)
{
字符串路径=System.Environment.GetEnvironmentVariable(“USERPROFILE”);
如果(!Directory.Exists(String.Concat(路径“\\desktop\\terriaserver\\filebin\\activeports.txt”))
{
Directory.CreateDirectory(String.Concat(路径“\\desktop\\terriaserver\\filebin”);
//使用(StreamWriter sw=File.CreateText(String.Concat(路径“\\desktop\\terriaserver\\filebin\\activeports.txt”){
}
如果(!Directory.Exists(String.Concat(路径“\\desktop\\terriaserver\\filebin\\chosenport.cmd”))
{
Directory.CreateDirectory(String.Concat(路径“\\desktop\\terriaserver\\filebin”);
使用(StreamWriter sw=File.CreateText(String.Concat(路径“\\desktop\\terriaserver\\filebin\\chosenport.cmd”){
}
如果(args.Length>0)
{
if(args[0]==“-noread”)
{
}
else if(args[0]==“-读取”| | args[0]==“-默认值”)
{
如果(!读取)
{
Read();
读=真;
}
}
}
其他的
{
如果(!读取)
{
Read();
读=真;
}
}
如果(参数长度>=3)
{
如果(参数[1]!=“-默认值”||参数[1]!=“0”)
{
int.TryParse(参数[1],输出温度);
如果(温度>1024&&temp<65535)
{
minval=温度;
}
}
如果(参数[2]!=“-默认值”||参数[2]!=“0”)
{
int.TryParse(参数[2],输出温度);
如果(温度>1024&&temp<65535)
{
maxval=温度;
}
}
}
RandGen();
}
公共静态void RandGen()
{
字符串路径=System.Environment.GetEnvironmentVariable(“USERPROFILE”);
Random rand=新的Random();
chosenPort=rand.Next(minval,maxval);
for(int i=0;iusing 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);
        }
    }
}