static SharedSettingsManager()
 {
     var properties = MyResources.DefaultPuttyProfile
         .Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
         .Skip(3)
         .Select(ParseRegExportLine)
         .Where(t => t != null)
         .ToDictionary(p => p.Item1, p => p.Item2);
     DefaultProfile = new SharedConnectionSettings("default", properties);
 }
 public void SetUp()
 {
     this.fileName = Guid.NewGuid().ToString("N");
     var ssm = new SharedSettingsManager();
     this.settings1 = ssm.GetOrCreate(Guid.NewGuid().ToString("N"));
     this.settings2 = ssm.GetOrCreate(Guid.NewGuid().ToString("N"));
 }
        public void Save(SharedConnectionSettings profile)
        {
            if (profile == null)
            {
                throw new ArgumentNullException("profile");
            }

            try
            {
                var profileKeyName = GetRegistryKeyName(profile.Name);
                using (var profileKey = Registry.CurrentUser
                    .CreateSubKey(profileKeyName, RegistryKeyPermissionCheck.ReadWriteSubTree))
                {
                    if (profileKey == null)
                    {
                        throw new ClientException(string.Format("Failed to create PuTTY profile registy entry {0}", profile.Name));
                    }

                    foreach (var pair in profile.Properties)
                    {
                        RegistryValueKind kind;
                        if (pair.Value is string)
                        {
                            kind = RegistryValueKind.String;
                        }
                        else if (pair.Value is int)
                        {
                            kind = RegistryValueKind.DWord;
                        }
                        else
                        {
                            throw new FormatException("Invalid shared settings property type.");
                        }

                        profileKey.SetValue(pair.Key, pair.Value, kind);
                    }
                }
            }
            catch (SecurityException e)
            {
                throw new ClientException(
                    string.Format("Security error has occured while updating the PuTTY profile {0}: {1}", profile.Name, e.Message),
                    e);
            }
            catch (UnauthorizedAccessException e)
            {
                throw new ClientException(
                    string.Format(
                        "Unauthorized access error has occured while updating PuTTY profile {0}: {1}",
                        profile.Name,
                        e.Message),
                    e);
            }
            catch (IOException e)
            {
                throw new ClientException(
                    string.Format("I/O error has occured while updating PuTTY profile {0}: {1}", profile.Name, e.Message),
                    e);
            }
        }
 private SharedConnectionSettings Create(string name)
 {
     var profile = new SharedConnectionSettings(name, new Dictionary<string, object>(DefaultProfile.Properties));
     this.Save(profile);
     return Read(profile.Name);
 }
        private static SharedConnectionSettings Read(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            try
            {
                var profileKeyName = GetRegistryKeyName(name);
                using (var profileKey = Registry.CurrentUser.OpenSubKey(profileKeyName, false))
                {
                    if (profileKey == null)
                    {
                        return null;
                    }

                    if (DefaultProfile.Properties.Keys.Except(profileKey.GetValueNames()).Any())
                    {
                        throw new ClientException("The default profile has some properties that are missing in the read one.");
                    }

                    var profile = new SharedConnectionSettings(name);
                    foreach (var propertyName in profileKey.GetValueNames())
                    {
                        var value = profileKey.GetValue(propertyName);
                        profile.Properties[propertyName] = value;
                    }

                    return profile;
                }
            }
            catch (SecurityException e)
            {
                throw new ClientException(
                    string.Format("Security error has occured while updating the PuTTY profile {0}: {1}", name, e.Message),
                    e);
            }
        }