Esempio n. 1
0
        public static IEnumerable <string> GetSetHostTransform(IEnumerable <string> contents, string hostName,
                                                               string address)
        {
            List <string> result = new List <string>();

            var needsInsert = true;

            foreach (var line in contents)
            {
                if (!HostsFileHelper.IsLineAHostFilesEntry(line))
                {
                    result.Add(line);
                    continue;
                }

                if (needsInsert)
                {
                    result.Add(GetHostLine(hostName, address));
                    needsInsert = false;
                }

                result.Add(line);
            }

            if (needsInsert)
            {
                result.Add(GetHostLine(hostName, address));
                needsInsert = false;
            }

            return(result);
        }
Esempio n. 2
0
        public static void Set(IEnumerable <HostsFileEntry> entries, string filepath = null)
        {
            filepath = filepath ?? GetHostsPath();

            List <Func <IEnumerable <string>, IEnumerable <string> > > transforms =
                new List <Func <IEnumerable <string>, IEnumerable <string> > >();

            foreach (var entry in entries.Reverse())
            {
                string hostName = entry.Hostname;
                string address  = entry.Address;
                transforms.Add(HostsFileHelper.GetRemoveTransformForHost(hostName));
                transforms.Add(lines => GetSetHostTransform(lines.ToArray(), hostName, address));
            }

            HostsFileHelper.TransformFile(filepath, transforms.ToArray());
        }
Esempio n. 3
0
        public static void Remove(Regex pattern)
        {
            var hostsPath = GetHostsPath();

            HostsFileHelper.RemoveFromFile(pattern, hostsPath);
        }
Esempio n. 4
0
        public static void Remove(string hostName)
        {
            var hostsPath = GetHostsPath();

            HostsFileHelper.RemoveFromFile(hostName, hostsPath);
        }