public void WritesFileCorrectly(bool enabled, string ident, string hostname, string address)
        {
            var file = Path.GetTempFileName();
            var testHost = new Host(enabled, ident, hostname, address);
            HostInterface.WriteHosts(file, new []{testHost});

            var readHosts = HostInterface.GetHosts(file);
            readHosts.Count().Should().Be(1);

            var readHost = readHosts.First();
            readHost.Enabled.Should().Be(enabled);
            readHost.Identifier.Should().Be(ident);
            readHost.Hostname.Should().Be(hostname);
            readHost.Address.Should().Be(address);

            File.Delete(file);
        }
        void AddHost(HostSwitcher switcher, Host host)
        {
            var menuItem = new ToolStripMenuItem
                {
                    Text = (string.IsNullOrWhiteSpace(host.Identifier) ? host.Hostname : host.Identifier),
                    Checked = host.Enabled
                };

            menuItem.Click += (s, a) =>
                {
                    host.Enabled = !menuItem.Checked;
                    menuItem.Checked = host.Enabled;
                    switcher.Save();
                };

            _contextMenu.Items.Add(menuItem);
        }
Example #3
0
        public static IEnumerable<Host> GetHosts(string HostsFile)
        {
            string[] hostsFile = File.ReadAllLines(HostsFile);

            {
                string host_id = null;
                foreach (var line in hostsFile)
                {
                    if (host_id == null) // First line of a 2 line host combo
                    {
                        if (line.StartsWith("##")) // This is a doco line for a host we've inserted
                        {
                            host_id = line.TrimStart(new[] {'#', ' ', '\t'});
                            continue;
                        }
                    }
                    else
                    {
                        bool enabled = !line.Trim().StartsWith("#");
                        var lineparts = line.Split(new[] {" ", "#"}, StringSplitOptions.RemoveEmptyEntries);
                        string host_address = lineparts[0].Replace("#", "");
                        string host_name = lineparts[1];
                        var host = new Host(enabled, host_id, host_name, host_address);
                        yield return host;

                        host_id = null;
                    }
                }
            }
        }