예제 #1
0
        public void WriteConfiguration()
        {
            FileStream   KeyFileStream   = new FileStream(DataFilePath + "key.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            FileStream   RangeFileStream = new FileStream(DataFilePath + "range.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            FileStream   AvoidFileStream = new FileStream(DataFilePath + "avoid.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            FileStream   MoreFileStream  = new FileStream(DataFilePath + "more.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter Key             = new StreamWriter(KeyFileStream);
            StreamWriter Range           = new StreamWriter(RangeFileStream);
            StreamWriter Avoid           = new StreamWriter(AvoidFileStream);
            StreamWriter More            = new StreamWriter(MoreFileStream);

            if (RangeChange)
            {
                Range.WriteLine(DataManage.Encrypt(NewMinValue.ToString(), NewKey));
                Range.WriteLine(DataManage.Encrypt(NewMaxValue.ToString(), NewKey));
                RangeChange = false;
                Range.Close();
            }
            if (AvoidChange)
            {
                foreach (int i in NewAvoidValues)
                {
                    Avoid.WriteLine(DataManage.Encrypt(i.ToString(), NewKey));
                }
                AvoidChange = false;
                Avoid.Close();
            }
            if (MoreChange)
            {
                More.WriteLine(DataManage.Encrypt(NewMoreRate.ToString(), NewKey));
                foreach (int i in NewMoreValues)
                {
                    More.WriteLine(DataManage.Encrypt(i.ToString(), NewKey));
                }
                MoreChange = false;
                More.Close();
            }
            if (KeyChange)
            {
                Key.WriteLine(NewKey);
                KeyChange = false;
                Key.Close();
            }
        }
예제 #2
0
        public Configurator()
        {
            FileStream KeyFileStream   = new FileStream(DataFilePath + "key.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            FileStream RangeFileStream = new FileStream(DataFilePath + "range.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            FileStream AvoidFileStream = new FileStream(DataFilePath + "avoid.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            FileStream MoreFileStream  = new FileStream(DataFilePath + "more.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite);

            string currentKey = "";

            using (StreamReader currentKeyReader = new StreamReader(KeyFileStream)) {
                currentKey = currentKeyReader.ReadLine();
                NewKey     = currentKey;
            }

            using (StreamReader currentRangeReader = new StreamReader(RangeFileStream)) {
                List <string> fileStrLines = new List <string>();
                while (!currentRangeReader.EndOfStream)
                {
                    fileStrLines.Add(currentRangeReader.ReadLine());
                }

                //判断范围文件为空
                if (fileStrLines.Count == 0)
                {
                    using (FileStream fs = new FileStream(DataFilePath + "range.conf", FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                        using (StreamWriter sw = new StreamWriter(fs)) {
                            sw.WriteLine("1");
                            sw.WriteLine("50");
                        }
                    }
                }
                else if (fileStrLines.Count == 2)
                {
                    bool convertSuccessful =
                        int.TryParse(fileStrLines[0], out NewMinValue) &&
                        int.TryParse(fileStrLines[1], out NewMaxValue);
                    if (!convertSuccessful || NewMinValue >= NewMaxValue)
                    {
                        NewMinValue = 1;
                        NewMaxValue = 50;
                        RangeChange = true;
                    }
                }
                else
                {
                    RangeChange = true;
                }
            }

            using (StreamReader currentAvoidReader = new StreamReader(AvoidFileStream)) {
                List <string> fileStrs = new List <string>();
                while (!currentAvoidReader.EndOfStream)
                {
                    fileStrs.Add(currentAvoidReader.ReadLine());
                }
                foreach (string str in fileStrs)
                {
                    if (int.TryParse(DataManage.Decrypt(str.ToCharArray(), currentKey), out int num))
                    {
                        NewAvoidValues.Add(num);
                    }
                }
            }

            using (StreamReader currentMoreReader = new StreamReader(MoreFileStream)) {
                List <string> fileStrs = new List <string>();
                while (!currentMoreReader.EndOfStream)
                {
                    fileStrs.Add(currentMoreReader.ReadLine());
                }

                if (fileStrs.Count <= 1)
                {
                    MoreChange = false;
                }
                else
                {
                    if (int.TryParse(DataManage.Decrypt(fileStrs[0].ToCharArray(), currentKey), out int rate))
                    {
                        NewMoreRate = rate;
                    }
                    for (int i = 1; i < fileStrs.Count; i++)
                    {
                        if (int.TryParse(DataManage.Decrypt(fileStrs[i].ToCharArray(), currentKey), out int num))
                        {
                            NewMoreValues.Add(num);
                        }
                    }
                }
            }
        }