public void SetHostFileContent(HostsEntryViewModel hostEntry) { ClearMessage(); IsBusy = true; try { //Create or Update host file content File.WriteAllText(Utilities.Hosts.HostFilePath, hostEntry.Content); //if (SelectedHostFile != HostFiles.First()) HostFiles.First().Content = GetHostFileContent(); if (SelectedHostFile.IsSystem) { IgnoreFileContentChange = true; Document.Text = HostFiles.First().Content; IgnoreFileContentChange = false; } SetMessage("Done! Hosts file content was updated.", MessageType.Success); string flushError; if (AutoFlushDns) { Utilities.Common.TryFlushDns(out flushError); } if (AutoLaunchUrls) { StartPredefinedUrls(); } } catch (UnauthorizedAccessException uaEx) { SetMessage(InvalidAccessMessage, MessageType.Failure, uaEx.Message); } catch (Exception ex) { SetMessage("Some unexpected issue occurred while updating the hosts file.", MessageType.Failure, ex.Message); } finally { IsBusy = false; } }
private void EnableHostFile() { ClearMessage(); IsBusy = true; try { //Check if hosts exist if (Utilities.Hosts.HostFileExists) { //if does skip and return SetMessage("Good to go! Hosts file already exists.", MessageType.Information); return; } //If there are no active hosts file and there is a previously disabled hosts file, rename it to hosts //Rename hosts_disabled to hosts if (File.Exists(_disabledHostFile)) { File.Move(_disabledHostFile, Utilities.Hosts.HostFilePath); } else //Create a new file with hosts name and dump the content as appears in the application { File.WriteAllText(Utilities.Hosts.HostFilePath, HostFiles.First().Content); } //Refresh host file content HostFiles.First().Content = GetHostFileContent(); if (SelectedHostFile.IsSystem) { Document.Text = SelectedHostFile.Content; SelectedHostFile.IsDirty = false; } SetMessage("Done! Your system's hosts file has been enabled.", MessageType.Success); string flushError; if (AutoFlushDns) { Utilities.Common.TryFlushDns(out flushError); } if (AutoLaunchUrls) { StartPredefinedUrls(); } } catch (UnauthorizedAccessException uaEx) { SetMessage(InvalidAccessMessage, MessageType.Failure, uaEx.Message); _isHostsEnabled = false; } catch (SecurityException secEx) { _isHostsEnabled = false; SetMessage("Can't enable the hosts. :( Looks like your user account is facing some security permissions issue.", MessageType.Failure, secEx.Message); } catch (IOException ioEx) { _isHostsEnabled = false; SetMessage("Can't enable the hosts. :( Looks like an internal disk related problem occurred.", MessageType.Failure, ioEx.Message); } catch (Exception ex) { _isHostsEnabled = false; SetMessage("An unexpected issue occurred while enabling hosts file.", MessageType.Failure, ex.Message); } finally { IsBusy = false; } }