public bool Save(ITest test, string testName, int subject)
        {
            if (testName == null)
            {
                throw new ArgumentNullException("Значение testName не может быть null");
            }
            FileProcessor.ClearTmpDir(tmpDir);

            for (var i = 0; i < test.Questions.Count; i++)
            {
                var serialized_question = test.Questions[i].Serialize();
                using (FileStream file = new FileStream(tmpDir + @"\" + i + ".dat", FileMode.OpenOrCreate))
                {
                    serialized_question.WriteTo(file);
                }
            }

            var formatter = new BinaryFormatter();

            using (FileStream file = new FileStream(tmpDir + @"\main.dat", FileMode.OpenOrCreate))
            {
                formatter.Serialize(file, test);
            }

            var stream = new MemoryStream();

            FileProcessor.CompressFile(tmpDir, stream);
            var testInfo = new NetSerializedTestInfo(stream.ToArray(), testName, subject);

            _error = null;
            connection.SendCommand(
                new RequestInfo(
                    "SaveTest",
                    SequrityUtils.Encrypt(
                        testInfo.ToJson(),
                        connection.User.SecretKey),
                    connection.User.UserToken),
                onSaveRecive
                );

            FileProcessor.ClearTmpDir(tmpDir);
            if (_error == null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private void onLoadRecive(string data)
        {
            loadedTest = null;
            var response = ResponseInfo.FromJson(data);

            if (response.Error != null)
            {
                _error = response.Error;
            }
            else
            {
                FileProcessor.ClearTmpDir(tmpDir);

                var testInfo = NetSerializedTestInfo.FromJson(SequrityUtils.DecryptString(response.Data, connection.User.SecretKey));
                using (FileStream file = new FileStream(tmpDir + @"\testtmp.test", FileMode.OpenOrCreate))
                {
                    file.Write(testInfo.Test, 0, testInfo.Test.Length);
                }

                FileProcessor.DecompressFile(tmpDir + @"\testtmp.test", "");
                var  formatter = new BinaryFormatter();
                Test test;
                using (FileStream file = new FileStream(tmpDir + @"\main.dat", FileMode.Open))
                {
                    test = (Test)formatter.Deserialize(file);
                    test.InitSerializedTest();
                }

                var i = 0;
                while (File.Exists(tmpDir + @"\" + i.ToString() + ".dat"))
                {
                    using (FileStream file = new FileStream(tmpDir + @"\" + i.ToString() + ".dat", FileMode.Open))
                    {
                        test.Questions.Add(new Question(file));
                    }
                    i++;
                }

                FileProcessor.ClearTmpDir(tmpDir);
                loadedTest = test;
            }
        }
        public ITest LoadForEdit(string fileName, string password = null)
        {
            _fileName = fileName;

            FileProcessor.ClearTmpDir(tmpDir);

            if (password == null)
            {
                FileProcessor.DecompressFile(fileName, "");
            }
            else
            {
                FileProcessor.EncryptDecryptFile(fileName, password, false, tmpDir + @"\testtmp.test");
                FileProcessor.DecompressFile(tmpDir + @"\testtmp.test", "");
            }

            var  formatter = new BinaryFormatter();
            Test test;

            using (FileStream file = new FileStream(tmpDir + @"\main.dat", FileMode.Open))
            {
                test = (Test)formatter.Deserialize(file);
                test.InitSerializedTest();
            }

            var i = 0;

            while (File.Exists(tmpDir + @"\" + i.ToString() + ".dat"))
            {
                using (FileStream file = new FileStream(tmpDir + @"\" + i.ToString() + ".dat", FileMode.Open))
                {
                    test.Questions.Add(new Question(file));
                }
                i++;
            }

            FileProcessor.ClearTmpDir(tmpDir);

            return(test);
        }
        public bool Save(ITest test, string testPath = null)
        {
            if (testPath == null)
            {
                using (var saveDialog = new SaveFileDialog())
                {
                    saveDialog.Filter = "Файлы тестов (.test)| *.test";
                    if (saveDialog.ShowDialog() == DialogResult.OK)
                    {
                        _fileName = saveDialog.FileName;
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            else
            {
                _fileName = testPath;
            }
            FileProcessor.ClearTmpDir(tmpDir);

            for (var i = 0; i < test.Questions.Count; i++)
            {
                var serialized_question = test.Questions[i].Serialize();
                using (FileStream file = new FileStream(tmpDir + @"\" + i + ".dat", FileMode.OpenOrCreate))
                {
                    serialized_question.WriteTo(file);
                }
            }

            var formatter = new BinaryFormatter();

            using (FileStream file = new FileStream(tmpDir + @"\main.dat", FileMode.OpenOrCreate))
            {
                formatter.Serialize(file, test);
            }

            FileProcessor.CompressFile(tmpDir, tmpDir + @"\testtmp.test");
            if (test.Params.Password != "")
            {
                if (File.Exists(_fileName))
                {
                    File.Delete(_fileName);
                }
                FileProcessor.EncryptDecryptFile(tmpDir + @"\testtmp.test", test.Params.Password, true, _fileName);
            }
            else
            {
                if (File.Exists(_fileName))
                {
                    File.Delete(_fileName);
                }
                File.Copy(tmpDir + @"\testtmp.test", _fileName);
            }

            FileProcessor.ClearTmpDir(tmpDir);

            return(true);
        }