diff --git a/mRemoteV1/Config/Putty/PuttySessionsRegistryProvider.cs b/mRemoteV1/Config/Putty/PuttySessionsRegistryProvider.cs index 05754b18..9c0c70d1 100644 --- a/mRemoteV1/Config/Putty/PuttySessionsRegistryProvider.cs +++ b/mRemoteV1/Config/Putty/PuttySessionsRegistryProvider.cs @@ -7,37 +7,29 @@ using System; using System.Collections.Generic; using System.Management; using System.Security.Principal; +using System.Web; namespace mRemoteNG.Config.Putty { public class PuttySessionsRegistryProvider : PuttySessionsProvider { - #region Private Fields private const string PuttySessionsKey = "Software\\SimonTatham\\PuTTY\\Sessions"; private static ManagementEventWatcher _eventWatcher; - #endregion #region Public Methods public override string[] GetSessionNames(bool raw = false) { - RegistryKey sessionsKey = Registry.CurrentUser.OpenSubKey(PuttySessionsKey); + var sessionsKey = Registry.CurrentUser.OpenSubKey(PuttySessionsKey); if (sessionsKey == null) { return new string[] {}; } - - List sessionNames = new List(); - foreach (string sessionName in sessionsKey.GetSubKeyNames()) + + var sessionNames = new List(); + foreach (var sessionName in sessionsKey.GetSubKeyNames()) { - if (raw) - { - sessionNames.Add(sessionName); - } - else - { - sessionNames.Add(System.Web.HttpUtility.UrlDecode(sessionName.Replace("+", "%2B"))); - } + sessionNames.Add(raw ? sessionName : HttpUtility.UrlDecode(sessionName.Replace("+", "%2B"))); } if (raw) @@ -60,31 +52,25 @@ namespace mRemoteNG.Config.Putty public override PuttySessionInfo GetSession(string sessionName) { - RegistryKey sessionsKey = Registry.CurrentUser.OpenSubKey(PuttySessionsKey); - if (sessionsKey == null) - { - return null; - } - - RegistryKey sessionKey = sessionsKey.OpenSubKey(sessionName); + var sessionsKey = Registry.CurrentUser.OpenSubKey(PuttySessionsKey); + + var sessionKey = sessionsKey?.OpenSubKey(sessionName); if (sessionKey == null) { return null; } sessionName = System.Web.HttpUtility.UrlDecode(sessionName.Replace("+", "%2B")); - - PuttySessionInfo sessionInfo = new PuttySessionInfo(); - sessionInfo.PuttySession = sessionName; - sessionInfo.Name = sessionName; - sessionInfo.Hostname = Convert.ToString(sessionKey.GetValue("HostName")); - sessionInfo.Username = Convert.ToString(sessionKey.GetValue("UserName")); - string protocol = Convert.ToString(sessionKey.GetValue("Protocol")); - if (protocol == null) - { - protocol = "ssh"; - } - switch (protocol.ToLowerInvariant()) + + var sessionInfo = new PuttySessionInfo + { + PuttySession = sessionName, + Name = sessionName, + Hostname = Convert.ToString(sessionKey.GetValue("HostName")), + Username = Convert.ToString(sessionKey.GetValue("UserName")) + }; + var protocol = Convert.ToString(sessionKey.GetValue("Protocol")) ?? "ssh"; + switch (protocol.ToLowerInvariant()) { case "raw": sessionInfo.Protocol = ProtocolType.RAW; @@ -95,18 +81,11 @@ namespace mRemoteNG.Config.Putty case "serial": return null; case "ssh": - object sshVersionObject = sessionKey.GetValue("SshProt"); + var sshVersionObject = sessionKey.GetValue("SshProt"); if (sshVersionObject != null) { - int sshVersion = Convert.ToInt32(sshVersionObject); - if (sshVersion >= 2) - { - sessionInfo.Protocol = ProtocolType.SSH2; - } - else - { - sessionInfo.Protocol = ProtocolType.SSH1; - } + var sshVersion = Convert.ToInt32(sshVersionObject); + sessionInfo.Protocol = sshVersion >= 2 ? ProtocolType.SSH2 : ProtocolType.SSH1; } else { @@ -133,9 +112,9 @@ namespace mRemoteNG.Config.Putty try { - string currentUserSid = WindowsIdentity.GetCurrent().User.Value; - string key = Convert.ToString(string.Join("\\", new[] {currentUserSid, PuttySessionsKey}).Replace("\\", "\\\\")); - WqlEventQuery query = new WqlEventQuery(string.Format("SELECT * FROM RegistryTreeChangeEvent WHERE Hive = \'HKEY_USERS\' AND RootPath = \'{0}\'", key)); + var currentUserSid = WindowsIdentity.GetCurrent().User.Value; + var key = Convert.ToString(string.Join("\\", currentUserSid, PuttySessionsKey).Replace("\\", "\\\\")); + var query = new WqlEventQuery($"SELECT * FROM RegistryTreeChangeEvent WHERE Hive = \'HKEY_USERS\' AND RootPath = \'{key}\'"); _eventWatcher = new ManagementEventWatcher(query); _eventWatcher.EventArrived += OnManagementEventArrived; _eventWatcher.Start(); @@ -158,11 +137,9 @@ namespace mRemoteNG.Config.Putty } #endregion - #region Private Methods private void OnManagementEventArrived(object sender, EventArrivedEventArgs e) { OnSessionChanged(new SessionChangedEventArgs()); } - #endregion } -} +} \ No newline at end of file diff --git a/mRemoteV1/Config/Putty/PuttySessionsXmingProvider.cs b/mRemoteV1/Config/Putty/PuttySessionsXmingProvider.cs index 6d19764b..8eb73177 100644 --- a/mRemoteV1/Config/Putty/PuttySessionsXmingProvider.cs +++ b/mRemoteV1/Config/Putty/PuttySessionsXmingProvider.cs @@ -5,6 +5,7 @@ using mRemoteNG.Messages; using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text.RegularExpressions; @@ -12,37 +13,28 @@ namespace mRemoteNG.Config.Putty { public class PuttySessionsXmingProvider : PuttySessionsProvider { - #region Private Fields private const string RegistrySessionNameFormat = "{0} [registry]"; private const string RegistrySessionNamePattern = "(.*)\\ \\[registry\\]"; private static readonly PuttySessionsRegistryProvider PuttySessionsRegistryProvider = new PuttySessionsRegistryProvider(); private static FileSystemWatcher _eventWatcher; - #endregion #region Public Methods public override string[] GetSessionNames(bool raw = false) { - string sessionsFolderPath = GetSessionsFolderPath(); + var sessionsFolderPath = GetSessionsFolderPath(); if (!Directory.Exists(sessionsFolderPath)) { return new string[] {}; } - List sessionNames = new List(); - foreach (string sessionName in Directory.GetFiles(sessionsFolderPath)) + var sessionNames = new List(); + foreach (var sessionName in Directory.GetFiles(sessionsFolderPath)) { - string _sessionFileName = Path.GetFileName(sessionName); - if (raw) - { - sessionNames.Add(_sessionFileName); - } - else - { - sessionNames.Add(System.Web.HttpUtility.UrlDecode(_sessionFileName.Replace("+", "%2B"))); - } + var sessionFileName = Path.GetFileName(sessionName); + sessionNames.Add(raw ? sessionFileName : System.Web.HttpUtility.UrlDecode(sessionFileName?.Replace("+", "%2B"))); } - - if (raw) + + if (raw) { if (!sessionNames.Contains("Default%20Settings")) // Do not localize { @@ -57,13 +49,9 @@ namespace mRemoteNG.Config.Putty } } - List registrySessionNames = new List(); - foreach (string sessionName in PuttySessionsRegistryProvider.GetSessionNames(raw)) - { - registrySessionNames.Add(string.Format(RegistrySessionNameFormat, sessionName)); - } - - sessionNames.AddRange(registrySessionNames); + var registrySessionNames = PuttySessionsRegistryProvider.GetSessionNames(raw).Select(sessionName => string.Format(RegistrySessionNameFormat, sessionName)).ToList(); + + sessionNames.AddRange(registrySessionNames); sessionNames.Sort(); return sessionNames.ToArray(); @@ -71,38 +59,36 @@ namespace mRemoteNG.Config.Putty public override PuttySessionInfo GetSession(string sessionName) { - string registrySessionName = GetRegistrySessionName(sessionName); + var registrySessionName = GetRegistrySessionName(sessionName); if (!string.IsNullOrEmpty(registrySessionName)) { return ModifyRegistrySessionInfo(PuttySessionsRegistryProvider.GetSession(registrySessionName)); } - - string sessionsFolderPath = GetSessionsFolderPath(); + + var sessionsFolderPath = GetSessionsFolderPath(); if (!Directory.Exists(sessionsFolderPath)) { return null; } - - string sessionFile = Path.Combine(sessionsFolderPath, sessionName); + + var sessionFile = Path.Combine(sessionsFolderPath, sessionName); if (!File.Exists(sessionFile)) { return null; } sessionName = System.Web.HttpUtility.UrlDecode(sessionName.Replace("+", "%2B")); - - SessionFileReader sessionFileReader = new SessionFileReader(sessionFile); - PuttySessionInfo sessionInfo = new PuttySessionInfo(); - sessionInfo.PuttySession = sessionName; - sessionInfo.Name = sessionName; - sessionInfo.Hostname = sessionFileReader.GetValue("HostName"); - sessionInfo.Username = sessionFileReader.GetValue("UserName"); - string protocol = sessionFileReader.GetValue("Protocol"); - if (protocol == null) - { - protocol = "ssh"; - } - switch (protocol.ToLowerInvariant()) + + var sessionFileReader = new SessionFileReader(sessionFile); + var sessionInfo = new PuttySessionInfo + { + PuttySession = sessionName, + Name = sessionName, + Hostname = sessionFileReader.GetValue("HostName"), + Username = sessionFileReader.GetValue("UserName") + }; + var protocol = sessionFileReader.GetValue("Protocol") ?? "ssh"; + switch (protocol.ToLowerInvariant()) { case "raw": sessionInfo.Protocol = ProtocolType.RAW; @@ -116,15 +102,8 @@ namespace mRemoteNG.Config.Putty object sshVersionObject = sessionFileReader.GetValue("SshProt"); if (sshVersionObject != null) { - int sshVersion = Convert.ToInt32(sshVersionObject); - if (sshVersion >= 2) - { - sessionInfo.Protocol = ProtocolType.SSH2; - } - else - { - sessionInfo.Protocol = ProtocolType.SSH1; - } + var sshVersion = Convert.ToInt32(sshVersionObject); + sessionInfo.Protocol = sshVersion >= 2 ? ProtocolType.SSH2 : ProtocolType.SSH1; } else { @@ -154,9 +133,11 @@ namespace mRemoteNG.Config.Putty try { - _eventWatcher = new FileSystemWatcher(GetSessionsFolderPath()); - _eventWatcher.NotifyFilter = (System.IO.NotifyFilters) (NotifyFilters.FileName | NotifyFilters.LastWrite); - _eventWatcher.Changed += OnFileSystemEventArrived; + _eventWatcher = new FileSystemWatcher(GetSessionsFolderPath()) + { + NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite + }; + _eventWatcher.Changed += OnFileSystemEventArrived; _eventWatcher.Created += OnFileSystemEventArrived; _eventWatcher.Deleted += OnFileSystemEventArrived; _eventWatcher.Renamed += OnFileSystemEventArrived; @@ -186,43 +167,31 @@ namespace mRemoteNG.Config.Putty #region Private Methods private static string GetPuttyConfPath() { - string puttyPath = ""; - if (mRemoteNG.Settings.Default.UseCustomPuttyPath) - { - puttyPath = Convert.ToString(mRemoteNG.Settings.Default.CustomPuttyPath); - } - else - { - puttyPath = App.Info.GeneralAppInfo.PuttyPath; - } + var puttyPath = ""; + puttyPath = mRemoteNG.Settings.Default.UseCustomPuttyPath ? Convert.ToString(mRemoteNG.Settings.Default.CustomPuttyPath) : App.Info.GeneralAppInfo.PuttyPath; return Path.Combine(Path.GetDirectoryName(puttyPath), "putty.conf"); } private static string GetSessionsFolderPath() { - string puttyConfPath = GetPuttyConfPath(); - PuttyConfFileReader sessionFileReader = new PuttyConfFileReader(puttyConfPath); - string basePath = Environment.ExpandEnvironmentVariables(sessionFileReader.GetValue("sshk&sess")); + var puttyConfPath = GetPuttyConfPath(); + var sessionFileReader = new PuttyConfFileReader(puttyConfPath); + var basePath = Environment.ExpandEnvironmentVariables(sessionFileReader.GetValue("sshk&sess")); return Path.Combine(basePath, "sessions"); } private static string GetRegistrySessionName(string sessionName) { - Regex regex = new Regex(RegistrySessionNamePattern); - - MatchCollection matches = regex.Matches(sessionName); + var regex = new Regex(RegistrySessionNamePattern); + + var matches = regex.Matches(sessionName); if (matches.Count < 1) { return string.Empty; } - - GroupCollection groups = matches[0].Groups; - if (groups.Count < 1) - { - return string.Empty; // This should always include at least one item, but check anyway - } - - return groups[1].Value; + + var groups = matches[0].Groups; + return groups.Count < 1 ? string.Empty : groups[1].Value; } private static PuttySessionInfo ModifyRegistrySessionInfo(PuttySessionInfo sessionInfo) @@ -264,12 +233,11 @@ namespace mRemoteNG.Config.Putty { return ; } - using (StreamReader streamReader = new StreamReader(_puttyConfFile)) + using (var streamReader = new StreamReader(_puttyConfFile)) { - string line = ""; - do + do { - line = streamReader.ReadLine(); + var line = streamReader.ReadLine(); if (line == null) { break; @@ -283,7 +251,7 @@ namespace mRemoteNG.Config.Putty { continue; // Comment } - string[] parts = line.Split(new char[] {'='}, 2); + var parts = line.Split(new[] {'='}, 2); if (parts.Length < 2) { continue; @@ -309,11 +277,7 @@ namespace mRemoteNG.Config.Putty { LoadConfiguration(); } - if (!_configuration.ContainsKey(setting)) - { - return string.Empty; - } - return _configuration[setting]; + return !_configuration.ContainsKey(setting) ? string.Empty : _configuration[setting]; } } @@ -339,15 +303,14 @@ namespace mRemoteNG.Config.Putty } using (StreamReader streamReader = new StreamReader(_sessionFile)) { - string line = ""; - do + do { - line = streamReader.ReadLine(); + var line = streamReader.ReadLine(); if (line == null) { break; } - string[] parts = line.Split(new char[] {'\\'}); + var parts = line.Split('\\'); if (parts.Length < 2) { continue; @@ -369,11 +332,7 @@ namespace mRemoteNG.Config.Putty { LoadSessionInfo(); } - if (!_sessionInfo.ContainsKey(setting)) - { - return string.Empty; - } - return _sessionInfo[setting]; + return !_sessionInfo.ContainsKey(setting) ? string.Empty : _sessionInfo[setting]; } } #endregion