internal static List <ASServer> GetAllServers() { var list = new List <ASServer>(); StringBuilder result = new StringBuilder(); var path = AppDomain.CurrentDomain.BaseDirectory; var xml = string.Format(@"{0}\\Servers.xml", path); foreach (XElement server in XElement.Load(xml).Elements("Server")) { var xmlServer = new ASServer() { Name = server.Attribute("name").Value }; foreach (XElement ipServer in server.Elements("ascomip")) { xmlServer.AsComIp = ipServer.Attribute("value").Value; } foreach (XElement ipServer in server.Elements("easybizip")) { xmlServer.EasyBizIp = ipServer.Attribute("value").Value; } list.Add(xmlServer); } return(list); }
internal static string GetCurrentEnv() { try { var OSInfo = Environment.OSVersion; string pathpart = "hosts"; if (OSInfo.Platform == PlatformID.Win32NT) { //is windows NT pathpart = "system32\\drivers\\etc\\hosts"; } string hostfile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), pathpart); string[] lines = File.ReadAllLines(hostfile); var currentEnv = string.Empty; if (lines.Any(s => s.Contains(ASCOM))) { for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains(ASCOM)) { currentEnv = lines[i]; } } File.WriteAllLines(hostfile, lines); } var hostFileAttributes = currentEnv.Split(' '); var xmlServer = new ASServer(); if (hostFileAttributes.Length == 3) { xmlServer = GetServer(hostFileAttributes[2].ToString().Replace("#", string.Empty)); } return(SetEnvMessage(xmlServer)); } catch (Exception ex) { return(ex.Message.Contains("is denied") ? "No permissions to update hosts file. Run as administrator" : ex.Message); } }
private string[] UpdateHostsFileValues(ASServer xmlServer, string hostfile, string entry, string[] lines, string site) { if (lines.ToArray().Any(s => s.Contains(site))) { for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains(site)) { lines = SetEnvValue(entry, lines, i, xmlServer.Name); } } File.WriteAllLines(hostfile, lines); } else if (!lines.Contains(entry) && !xmlServer.Name.Equals(Prod_Name)) { File.AppendAllLines(hostfile, new String[] { entry }); } return(lines); }
private static string SetEnvMessage(ASServer xmlServer) { return(xmlServer.Name != null?string.Format("{0}: Ascom: ({1}) Easybiz: ({2})", xmlServer.Name, xmlServer.AsComIp, xmlServer.EasyBizIp) : "Prod: Ascom and EazyBiz"); }