BF2Statistics.Gamespy.Redirector.HostsFileIcs.HostsFileIcs C# (CSharp) Method

HostsFileIcs() public method

Creates a new instance of HostsFileIcs
public HostsFileIcs ( ) : System
return System
        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();
        }
HostsFileIcs