/// <summary>
        /// Creates a new instance of HostsFileIcs
        /// </summary>
        public HostsFileIcs() : base()
        {
            // We dont know?
            try
            {
                // Get the Hosts file object
                HostFile = new FileInfo(FilePath);

                // Make sure file exists
                if (!HostFile.Exists)
                {
                    HostFile.Open(FileMode.Create).Close();
                }

                // If HOSTS file is readonly, remove that attribute!
                if (HostFile.IsReadOnly)
                {
                    try
                    {
                        HostFile.IsReadOnly = false;
                    }
                    catch (Exception e)
                    {
                        Program.ErrorLog.Write("HOSTS.ics file is READONLY, Attribute cannot be removed: " + e.Message);
                        LastException = e;
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                Program.ErrorLog.Write("Program cannot access HOSTS.ics file in any way: " + e.Message);
                LastException = e;
                return;
            }

            // Make sure we can read the file amd write to it!
            try
            {
                // Get the hosts file contents
                using (StreamReader Rdr = new StreamReader(HostFile.OpenRead()))
                {
                    CanRead = true;
                    while (!Rdr.EndOfStream)
                    {
                        OrigContents.Add(Rdr.ReadLine());
                    }
                }
            }
            catch (Exception e)
            {
                Program.ErrorLog.Write("Unable to READ the HOSTS.ics file: " + e.Message);
                LastException = e;
                return;
            }

            // Check that we can write to the hosts file
            try
            {
                using (FileStream Stream = HostFile.OpenWrite())
                    CanWrite = true;
            }
            catch (Exception e)
            {
                Program.ErrorLog.Write("Unable to WRITE to the HOSTS.ics file: " + e.Message);
                LastException = e;
                return;
            }

            // Parse file entries
            base.ParseEntries();
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new instance of SysHostsFile
        /// </summary>
        public SysHostsFile() : base()
        {
            // We dont know?
            IsLocked = false;
            try
            {
                // Get the Hosts file object
                HostFile = new FileInfo(FilePath);

                // If HOSTS file is readonly, remove that attribute!
                if (HostFile.IsReadOnly)
                {
                    try
                    {
                        HostFile.IsReadOnly = false;
                    }
                    catch (Exception e)
                    {
                        TraceLog.TraceError("HOSTS file is READONLY, Attribute cannot be removed: " + e.Message);
                        LastException = e;
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                TraceLog.TraceError("Program cannot access HOSTS file in any way: " + e.Message);
                LastException = e;
                return;
            }

            // Try to get the Access Control for the hosts file
            try
            {
                Security = HostFile.GetAccessControl();
            }
            catch (Exception e)
            {
                TraceLog.TraceError("Unable to get HOSTS file Access Control: " + e.Message);
                LastException = e;
                return;
            }

            // Make sure we can read the file amd write to it!
            try
            {
                // Unlock hosts file
                if (!UnLock())
                {
                    return;
                }

                // Get the hosts file contents
                using (StreamReader Rdr = new StreamReader(HostFile.OpenRead()))
                {
                    while (!Rdr.EndOfStream)
                    {
                        OrigContents.Add(Rdr.ReadLine());
                    }
                }

                CanRead = true;
            }
            catch (Exception e)
            {
                TraceLog.TraceError("Unable to READ the HOSTS file: " + e.Message);
                LastException = e;
                return;
            }

            // Check that we can write to the hosts file
            try
            {
                using (FileStream Stream = HostFile.OpenWrite())
                    CanWrite = true;
            }
            catch (Exception e)
            {
                TraceLog.TraceError("Unable to WRITE to the HOSTS file: " + e.Message);
                LastException = e;
                return;
            }

            // Parse file entries
            base.ParseEntries();
        }