private object Get(string key, Type targetType = null, object defaultValue = null, SerializationMode serializationMode = SerializationMode.Xml) { try { if (targetType == null) { targetType = ConvertSettingsType(Check.TryCatch <SettingsType, Exception>(() => settingsStore.GetPropertyType(collectionPath, key))); } object result = null; if (targetType.IsEnum) { targetType = typeof(int); } if (targetType == typeof(bool)) { result = settingsStore.GetBoolean(collectionPath, key, Convert.ToBoolean(defaultValue)); } else if (targetType == typeof(string)) { result = settingsStore.GetString(collectionPath, key, defaultValue as string); } else if (targetType == typeof(int)) { result = settingsStore.GetInt32(collectionPath, key, Convert.ToInt32(defaultValue)); } else { if (settingsStore.PropertyExists(collectionPath, key)) { if (serializationMode == SerializationMode.Xml) { string xmlContent = settingsStore.GetString(collectionPath, key); var serializer = new XmlSerializer(targetType); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlContent)); var res = serializer.Deserialize(ms); return(res); } else { var ms = settingsStore.GetMemoryStream(collectionPath, key); if (ms != null) { var serializer = new BinaryFormatter(); result = serializer.Deserialize(ms); } } } } return(result ?? defaultValue); } catch (Exception) { return(defaultValue); } }
///////////////////////////////////////////////////////////////////////////// // Overridden Package Implementation #region Package Members /// <summary> /// Initialization of the package; this method is called right after the package is sited, /// so this is the place where you can put all the initialization code that rely on services /// provided by VisualStudio. /// </summary> protected override void Initialize() { Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); base.Initialize(); SettingsManager = new ShellSettingsManager(this); WritableSettingsStore userSettingsStore = SettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings); if (userSettingsStore.PropertyExists("Settings", "Params")) { try { Params = new CommentReflowerLib.ParameterSet(userSettingsStore.GetMemoryStream("Settings", "Params")); } catch (Exception) { ShowMessageBox("Comment Reflower Error", "Unable to read user settings. Resetting to default."); Params = new CommentReflowerLib.ParameterSet(); } } else { Params = new CommentReflowerLib.ParameterSet(); } // Add our command handlers for menu (commands must exist in the .vsct file) var mcs = (OleMenuCommandService)GetService(typeof(IMenuCommandService)); CommandID menuCommandID; OleMenuCommand menuItem; menuCommandID = new CommandID(GuidList.guidCommentReflowerCmdSet, PkgCmdIDList.cmdidAlignParameters); menuItem = new OleMenuCommand(MenuItemCallback, null, QueryStatusCallback, menuCommandID); mcs.AddCommand(menuItem); menuCommandID = new CommandID(GuidList.guidCommentReflowerCmdSet, PkgCmdIDList.cmdidReflowPoint); menuItem = new OleMenuCommand(MenuItemCallback, null, QueryStatusCallback, menuCommandID); mcs.AddCommand(menuItem); menuCommandID = new CommandID(GuidList.guidCommentReflowerCmdSet, PkgCmdIDList.cmdidReflowSelection); menuItem = new OleMenuCommand(MenuItemCallback, null, QueryStatusCallback, menuCommandID); mcs.AddCommand(menuItem); menuCommandID = new CommandID(GuidList.guidCommentReflowerCmdSet, PkgCmdIDList.cmdidSettings); menuItem = new OleMenuCommand(MenuItemCallback, menuCommandID); mcs.AddCommand(menuItem); }
protected static object GetPropertyIfExists(WritableSettingsStore settingsStore, string collectionName, string propertyName) { Object obj = null; if (settingsStore == null || !(collectionName.Length > 0) || !(propertyName.Length > 0)) { return(null); } if (settingsStore.PropertyExists(collectionName, propertyName)) { MemoryStream mstream = settingsStore.GetMemoryStream(collectionName, propertyName); obj = DeserializeFromStream(mstream); } return(obj); }
private void MainThreadInitialization(OleMenuCommandService menuCommandService, bool isAsyncPath) { SettingsManager = new ShellSettingsManager(this); WritableSettingsStore userSettingsStore = SettingsManager.GetWritableSettingsStore(SettingsScope.UserSettings); if (userSettingsStore.PropertyExists("Settings", "Params")) { try { Params = new CommentReflowerLib.ParameterSet(userSettingsStore.GetMemoryStream("Settings", "Params")); } catch (Exception) { ShowMessageBox("Comment Reflower Error", "Unable to read user settings. Resetting to default."); Params = new CommentReflowerLib.ParameterSet(); } } else { Params = new CommentReflowerLib.ParameterSet(); } // Add our command handlers for menu (commands must exist in the .vsct file) var mcs = menuCommandService; CommandID menuCommandID; OleMenuCommand menuItem; menuCommandID = new CommandID(GuidList.guidCommentReflowerCmdSet, PkgCmdIDList.cmdidAlignParameters); menuItem = new OleMenuCommand(MenuItemCallback, null, QueryStatusCallback, menuCommandID); mcs.AddCommand(menuItem); menuCommandID = new CommandID(GuidList.guidCommentReflowerCmdSet, PkgCmdIDList.cmdidReflowPoint); menuItem = new OleMenuCommand(MenuItemCallback, null, QueryStatusCallback, menuCommandID); mcs.AddCommand(menuItem); menuCommandID = new CommandID(GuidList.guidCommentReflowerCmdSet, PkgCmdIDList.cmdidReflowSelection); menuItem = new OleMenuCommand(MenuItemCallback, null, QueryStatusCallback, menuCommandID); mcs.AddCommand(menuItem); menuCommandID = new CommandID(GuidList.guidCommentReflowerCmdSet, PkgCmdIDList.cmdidSettings); menuItem = new OleMenuCommand(MenuItemCallback, menuCommandID); mcs.AddCommand(menuItem); }
internal void Load() { try { var collectionName = typeof(Settings).FullName; // _writableSettingsStore.DeleteCollection(collectionName); if (!_writableSettingsStore.CollectionExists(collectionName)) { Save(); } var settings = new SharpSerializerBinarySettings(BinarySerializationMode.SizeOptimized) { IncludeAssemblyVersionInTypeName = false, IncludeCultureInTypeName = false, IncludePublicKeyTokenInTypeName = false }; var serializer = new SharpSerializer(settings); var xml = _writableSettingsStore.GetMemoryStream(collectionName, nameof(Items)); FromSerializable((serializer.Deserialize(xml) as SerializableSettings)); } catch (Exception) { //Debug.Fail(ex.Message); } }