示例#1
0
        private void Hosts2Grid(String hosts)
        {
            List <HostRow> list = new List <HostRow>();

            Int32 indexLine = 1;

            String[] lines = hosts.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            foreach (String line in lines)
            {
                if (!String.IsNullOrEmpty(line.Trim()))
                {
                    Match matches = Regex.Match(line.Trim(), _HostsLine);
                    if (matches.Success)
                    {
                        HostRow row = new HostRow();
                        row.LineIndex  = indexLine;
                        row.Enabled    = matches.Groups["enabled"].Value != "#";
                        row.IPAddress  = matches.Groups["ip"].Value;
                        row.DomainName = matches.Groups["domain"].Value;
                        row.Comment    = matches.Groups["comment"].Value;
                        list.Add(row);
                    }
                }
                indexLine++;
            }

            HostsDataGridView.DataSource = list;
        }
示例#2
0
        public void AddHostToFileTest()
        {
            DataManager dataManager = new DataManager();
            HostRow     testData    = new HostRow(0, "127.0.3.1 test.data");

            dataManager.path = "hostsTestData.txt";

            Assert.IsTrue(dataManager.AddHostToFile(testData));
        }
示例#3
0
        /// <summary>
        /// Adds a new host into the `hosts` file.
        /// </summary>
        /// <param name="hostRow">A HostRow object containing the IP address and Hostname to add to the file</param>
        /// <returns>Whether the new host could be added to the file or not.</returns>
        public bool AddHostToFile(HostRow hostRow)
        {
            try
            {
                File.AppendAllText(path, $"{hostRow.IP} {hostRow.Host}" + Environment.NewLine);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }

            return(true);
        }
示例#4
0
        public void UpdateHostTest()
        {
            DataManager dataManager = new DataManager();
            HostRow     testData    = new HostRow(1, "127.0.0.11", "iaero.me.local.test");

            dataManager.path = "hostsTestData.txt";
            dataManager.LoadHostsFromFile();

            Assert.IsTrue(dataManager.UpdateHost(testData));

            dataManager.LoadHostsFromFile();

            Assert.AreEqual("127.0.0.11", dataManager.Hosts[0].IP);
            Assert.AreEqual("iaero.me.local.test", dataManager.Hosts[0].Host);
        }
示例#5
0
        /// <summary>
        /// Updates a host entry in the `hosts` file.
        /// </summary>
        /// <param name="hostRow">The HostRow to update</param>
        /// <returns>Whether the host was able to be updated or not.</returns>
        public bool UpdateHost(HostRow hostRow)
        {
            try
            {
                string[] lines = File.ReadAllLines(path);

                lines[hostRow.FileRow] = $"{hostRow.IP} {hostRow.Host}" + Environment.NewLine;
                File.WriteAllLines(path, lines);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return(false);
            }

            return(true);
        }
示例#6
0
        private void Hosts2Grid(String hosts)
        {
            List<HostRow> list = new List<HostRow>();

            Int32 indexLine = 1;

            String[] lines = hosts.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            foreach (String line in lines)
            {
                if (!String.IsNullOrEmpty(line.Trim()))
                {
                    Match matches = Regex.Match(line.Trim(), _HostsLine);
                    if (matches.Success)
                    {
                        HostRow row = new HostRow();
                        row.LineIndex = indexLine;
                        row.Enabled = matches.Groups["enabled"].Value != "#";
                        row.IPAddress = matches.Groups["ip"].Value;
                        row.DomainName = matches.Groups["domain"].Value;
                        row.Comment = matches.Groups["comment"].Value;
                        list.Add(row);
                    }
                }
                indexLine++;
            }

            HostsDataGridView.DataSource = list;
        }