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);
            }
        }
Exemplo n.º 2
0
        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);
        }