1 //------------------------------------------------------------------------------
2 // <copyright file="WritableSettingsStore.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace Microsoft
.VisualStudio
.Shell
.Settings
11 using System
.Collections
.Generic
;
12 using Microsoft
.VisualStudio
.Shell
.Interop
;
13 using System
.Runtime
.InteropServices
;
14 using System
.Diagnostics
;
15 using Microsoft
.VisualStudio
.Settings
;
18 /// Abstract class for both reading and writing the selected scope's collections and properties. It is obtained from
19 /// <see cref="SettingsManager.GetWritableSettingsStore"/> method.
21 /// This class is derived from the SettingsStore hence it inherits all the functionalities from it. It adds property and
22 /// collection manipulation abilities on top of it.
24 [CLSCompliant(false)] // Methods of this class have unsigned integer parameters
25 internal sealed class ShellWritableSettingsStore
: WritableSettingsStore
27 #region SettingsStore implementation
28 /// The overrides to the SettingsStore methods are delegated to an instance of
29 /// the ShellSettingsStore class.
31 public override bool GetBoolean(string collectionPath
, string propertyName
)
33 return settingsStore
.GetBoolean(collectionPath
, propertyName
);
36 public override bool GetBoolean(string collectionPath
, string propertyName
, bool defaultValue
)
38 return settingsStore
.GetBoolean(collectionPath
, propertyName
, defaultValue
);
41 public override int GetInt32(string collectionPath
, string propertyName
)
43 return settingsStore
.GetInt32(collectionPath
, propertyName
);
46 public override int GetInt32(string collectionPath
, string propertyName
, int defaultValue
)
48 return settingsStore
.GetInt32(collectionPath
, propertyName
, defaultValue
);
51 public override uint GetUInt32(string collectionPath
, string propertyName
)
53 return settingsStore
.GetUInt32(collectionPath
, propertyName
);
56 public override uint GetUInt32(string collectionPath
, string propertyName
, uint defaultValue
)
58 return settingsStore
.GetUInt32(collectionPath
, propertyName
, defaultValue
);
61 public override long GetInt64(string collectionPath
, string propertyName
)
63 return settingsStore
.GetInt64(collectionPath
, propertyName
);
66 public override long GetInt64(string collectionPath
, string propertyName
, long defaultValue
)
68 return settingsStore
.GetInt64(collectionPath
, propertyName
, defaultValue
);
71 public override ulong GetUInt64(string collectionPath
, string propertyName
)
73 return settingsStore
.GetUInt64(collectionPath
, propertyName
);
76 public override ulong GetUInt64(string collectionPath
, string propertyName
, ulong defaultValue
)
78 return settingsStore
.GetUInt64(collectionPath
, propertyName
, defaultValue
);
81 public override string GetString(string collectionPath
, string propertyName
)
83 return settingsStore
.GetString(collectionPath
, propertyName
);
86 public override string GetString(string collectionPath
, string propertyName
, string defaultValue
)
88 return settingsStore
.GetString(collectionPath
, propertyName
, defaultValue
);
91 public override MemoryStream
GetMemoryStream(string collectionPath
, string propertyName
)
93 return settingsStore
.GetMemoryStream(collectionPath
, propertyName
);
96 public override SettingsType
GetPropertyType(string collectionPath
, string propertyName
)
98 return settingsStore
.GetPropertyType(collectionPath
, propertyName
);
101 public override bool PropertyExists(string collectionPath
, string propertyName
)
103 return settingsStore
.PropertyExists(collectionPath
, propertyName
);
106 public override bool CollectionExists(string collectionPath
)
108 return settingsStore
.CollectionExists(collectionPath
);
111 public override DateTime
GetLastWriteTime(string collectionPath
)
113 return settingsStore
.GetLastWriteTime(collectionPath
);
116 public override int GetSubCollectionCount(string collectionPath
)
118 return settingsStore
.GetSubCollectionCount(collectionPath
);
121 public override int GetPropertyCount(string collectionPath
)
123 return settingsStore
.GetPropertyCount(collectionPath
);
126 public override IEnumerable
<string> GetSubCollectionNames(string collectionPath
)
128 return settingsStore
.GetSubCollectionNames(collectionPath
);
131 public override IEnumerable
<string> GetPropertyNames(string collectionPath
)
133 return settingsStore
.GetPropertyNames(collectionPath
);
138 #region WritableSettingsStore implementation
141 /// Updates the value of the specified property to the given Boolean value while setting its data type to
142 /// <see cref="SettingsType.Int32"/>. If the previous data type of the property is different, it overwrites it.
143 /// If the property does not exist it creates one.
145 /// <param name="collectionPath">Path of the collection of the property.</param>
146 /// <param name="propertyName">Name of the property.</param>
147 /// <param name="value">New value of the property.</param>
148 /// <exception cref="ArgumentException">If the collection does not exist, this exception is thrown.</exception>
149 public override void SetBoolean(string collectionPath
, string propertyName
, bool value)
151 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
152 HelperMethods
.CheckNullArgument(propertyName
, "propertyName");
154 int hr
= this.writableSettingsStore
.SetBool(collectionPath
, propertyName
, Convert
.ToInt32(value));
155 Marshal
.ThrowExceptionForHR(hr
);
159 /// Updates the value of the specified property to the given integer value while setting its data type to
160 /// <see cref="SettingsType.Int32"/>. If the previous data type of the property is different, it overwrites it.
161 /// If the property does not exist it creates one.
163 /// <param name="collectionPath">Path of the collection of the property.</param>
164 /// <param name="propertyName">Name of the property.</param>
165 /// <param name="value">New value of the property.</param>
166 /// <exception cref="ArgumentException">If the collection does not exist, this exception is thrown.</exception>
167 public override void SetInt32(string collectionPath
, string propertyName
, int value)
169 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
170 HelperMethods
.CheckNullArgument(propertyName
, "propertyName");
172 int hr
= this.writableSettingsStore
.SetInt(collectionPath
, propertyName
, value);
173 Marshal
.ThrowExceptionForHR(hr
);
177 /// Updates the value of the specified property to the given unsigned integer value while setting its data type to
178 /// <see cref="SettingsType.Int32"/>. If the previous data type of the property is different, it overwrites it.
179 /// If the property does not exist it creates one.
181 /// <param name="collectionPath">Path of the collection of the property.</param>
182 /// <param name="propertyName">Name of the property.</param>
183 /// <param name="value">New value of the property.</param>
184 /// <exception cref="ArgumentException">If the collection does not exist, this exception is thrown.</exception>
185 public override void SetUInt32(string collectionPath
, string propertyName
, uint value)
187 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
188 HelperMethods
.CheckNullArgument(propertyName
, "propertyName");
190 int hr
= this.writableSettingsStore
.SetUnsignedInt(collectionPath
, propertyName
, value);
191 Marshal
.ThrowExceptionForHR(hr
);
196 /// Updates the value of the specified property to the given long value while setting its data type to
197 /// <see cref="SettingsType.Int64"/>. If the previous data type of the property is different, it overwrites it.
198 /// If the property does not exist it creates one.
200 /// <param name="collectionPath">Path of the collection of the property.</param>
201 /// <param name="propertyName">Name of the property.</param>
202 /// <param name="value">New value of the property.</param>
203 /// <exception cref="ArgumentException">If the collection does not exist, this exception is thrown.</exception>
204 public override void SetInt64(string collectionPath
, string propertyName
, long value)
206 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
207 HelperMethods
.CheckNullArgument(propertyName
, "propertyName");
209 int hr
= this.writableSettingsStore
.SetInt64(collectionPath
, propertyName
, value);
210 Marshal
.ThrowExceptionForHR(hr
);
214 /// Updates the value of the specified property to the given unsigned long value while setting its data type to
215 /// <see cref="SettingsType.Int64"/>. If the previous data type of the property is different, it overwrites it.
216 /// If the property does not exist it creates one.
218 /// <param name="collectionPath">Path of the collection of the property.</param>
219 /// <param name="propertyName">Name of the property.</param>
220 /// <param name="value">New value of the property.</param>
221 /// <exception cref="ArgumentException">If the collection does not exist, this exception is thrown.</exception>
222 public override void SetUInt64(string collectionPath
, string propertyName
, ulong value)
224 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
225 HelperMethods
.CheckNullArgument(propertyName
, "propertyName");
227 int hr
= this.writableSettingsStore
.SetUnsignedInt64(collectionPath
, propertyName
, value);
228 Marshal
.ThrowExceptionForHR(hr
);
232 /// Updates the value of the specified property to the given string value while setting its data type to
233 /// <see cref="SettingsType.String"/>. If the previous data type of the property is different, it overwrites it.
234 /// If the property does not exist it creates one.
236 /// <param name="collectionPath">Path of the collection of the property.</param>
237 /// <param name="propertyName">Name of the property.</param>
238 /// <param name="value">New value of the property.</param>
239 /// <exception cref="ArgumentException">If the collection does not exist, this exception is thrown.</exception>
240 public override void SetString(string collectionPath
, string propertyName
, string value)
242 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
243 HelperMethods
.CheckNullArgument(propertyName
, "propertyName");
244 HelperMethods
.CheckNullArgument(value, "value");
246 int hr
= this.writableSettingsStore
.SetString(collectionPath
, propertyName
, value);
247 Marshal
.ThrowExceptionForHR(hr
);
251 /// Updates the value of the specified property to the bits of the MemoryStream while setting its data type to
252 /// <see cref="SettingsType.Binary"/>. If the previous data type of the property is different, it overwrites it.
253 /// If the property does not exist it creates one.
255 /// <param name="collectionPath">Path of the collection of the property.</param>
256 /// <param name="propertyName">Name of the property.</param>
257 /// <param name="value">MemoryStream to set the bits of the property.</param>
258 /// <exception cref="ArgumentException">If the collection does not exist, this exception is thrown.</exception>
259 public override void SetMemoryStream(string collectionPath
, string propertyName
, MemoryStream
value)
261 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
262 HelperMethods
.CheckNullArgument(propertyName
, "propertyName");
263 HelperMethods
.CheckNullArgument(value, "value");
265 byte[] valueBuffer
= value.ToArray();
266 int hr
= this.writableSettingsStore
.SetBinary(collectionPath
, propertyName
, (uint)valueBuffer
.Length
, valueBuffer
);
267 Marshal
.ThrowExceptionForHR(hr
);
271 /// Creates the given collection path by creating each nested collection while skipping the ones that already exist.
272 /// If the full path of collections already exist, the method simply returns.
274 /// <param name="collectionPath">Path of the collection.</param>
275 /// <exception cref="ArgumentException">If empty string ("") is passed to the method then it throws this exception.
277 public override void CreateCollection(string collectionPath
)
279 HelperMethods
.CheckNullOrEmptyString(collectionPath
, "collectionPath");
281 int hr
= this.writableSettingsStore
.CreateCollection(collectionPath
);
282 Marshal
.ThrowExceptionForHR(hr
);
286 /// Deletes the given collection recursively deleting all of the sub collections and properties in it. If the collection
287 /// does not exist or an empty string ("") is passed then the method returns false.
289 /// <param name="collectionPath">Path of the collection to be deleted.</param>
290 /// <returns>Result of the deletion.</returns>
291 public override bool DeleteCollection(string collectionPath
)
293 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
295 int hr
= this.writableSettingsStore
.DeleteCollection(collectionPath
);
296 Marshal
.ThrowExceptionForHR(hr
);
298 Debug
.Assert(hr
== VSConstants
.S_OK
|| hr
== VSConstants
.S_FALSE
);
300 return hr
== VSConstants
.S_OK
;
304 /// Deletes the given property from the collection. If the property or the collection does not exist then the method
307 /// <param name="collectionPath">Collection that contains the property to be deleted.</param>
308 /// <param name="propertyName">Name of the property.</param>
309 /// <returns>Result of the deletion.</returns>
310 public override bool DeleteProperty(string collectionPath
, string propertyName
)
312 HelperMethods
.CheckNullArgument(collectionPath
, "collectionPath");
313 HelperMethods
.CheckNullArgument(propertyName
, "propertyName");
315 int hr
= this.writableSettingsStore
.DeleteProperty(collectionPath
, propertyName
);
316 Marshal
.ThrowExceptionForHR(hr
);
318 Debug
.Assert(hr
== VSConstants
.S_OK
|| hr
== VSConstants
.S_FALSE
);
320 return hr
== VSConstants
.S_OK
;
326 /// Internal constructor that takes the COM interface that provides the functionality of this class.
328 /// <param name="writableSettingsStore">COM interface wrapped by this class.</param>
329 internal ShellWritableSettingsStore(IVsWritableSettingsStore writableSettingsStore
)
331 HelperMethods
.CheckNullArgument(writableSettingsStore
, "writableSettingsStore");
332 this.writableSettingsStore
= writableSettingsStore
;
333 this.settingsStore
= new ShellSettingsStore(this.writableSettingsStore
);
336 // Thunked interop COM interface.
337 private IVsWritableSettingsStore writableSettingsStore
;
339 // Thunked SettingsStore instance.
340 private SettingsStore settingsStore
;