Exemplo n.º 1
0
        public IActionResult EditPgHba()
        {
            var pgHbas = _config.GetSection("AppSettings:UserTab:Pg_hba:pg_hbaPaths").Get <List <string> >();
            UserPgHbaViewModel model     = new UserPgHbaViewModel();
            string             pghbaText = string.Empty;
            string             fileDate  = string.Empty;

            try
            {
                pghbaText = System.IO.File.ReadAllText(pgHbas[0]);
                fileDate  = GetFileDate(pgHbas[0]);
            }

            catch (FileNotFoundException)
            {
                pghbaText = "Файл не найден";
            }
            catch (DirectoryNotFoundException)
            {
                pghbaText = "Файл не найден";
            }
            catch (Exception e)
            {
                pghbaText = e.Message;
                ModelState.AddModelError("", e.Message);
            }

            model.PgHbaContext = pghbaText;
            model.FileDate     = fileDate;

            return(PartialView(model));
        }
Exemplo n.º 2
0
        public IActionResult EditPgHba(UserPgHbaViewModel model)
        {
            string pghbaText = model.PgHbaContext;
            var    pgHbas    = _config.GetSection("AppSettings:UserTab:Pg_hba:pg_hbaPaths").Get <List <string> >();

            string errors = string.Empty;

            foreach (string pghba in pgHbas)
            {
                try
                {
                    using (var fileStream = new FileStream(pghba, FileMode.Create, FileAccess.Write))
                    {
                        Encoding encodingUtf8WoBOM = new UTF8Encoding(false);
                        using (var streamWriter = new StreamWriter(fileStream, encodingUtf8WoBOM))
                        {
                            streamWriter.Write(pghbaText);
                        }
                    }
                }
                catch (Exception e)
                {
                    errors += $"Не удалось сохранить файл конфигурации {pghba}. ({e.Message}).\n";
                }
            }

            if (errors.Length > 0)
            {
                ModelState.AddModelError("", errors);
            }

            string fileDate = string.Empty;

            try
            {
                fileDate = GetFileDate(pgHbas[0]);
            }
            catch
            {
            }
            model.FileDate = fileDate;

            return(PartialView(model));
        }