|
Hi All, Does anyone know how to prevent the SettingsProvider from creating an application version folder? I noticed when my class inherits from SettingsProvider, a folder called "5.0.0.0" (as an example) is created in the same level as the "app.settings" file path. It seems to happen when my CustomSettingsProvider class is initialized or when its SetPropertyValues is called. Thanks. --Lenard - Changed TypeAland LiMSFT, ModeratorFriday, September 11, 2009 6:29 AMNo reply
- Changed TypeLenardd Saturday, September 12, 2009 7:26 AM
-
|
| Lenardd Saturday, September 05, 2009 8:54 PM |
It is because you use Application.CommonAppDataPath. Reading the property will cause the directory to be created as a side-effect. Just remove the code that reads and deletes the directory and it shouldn't be created anymore. Assuming there is no other code in your app that uses the property.
Hans Passant.- Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 2:17 AM
-
|
| nobugz Monday, September 14, 2009 12:37 PM |
Hi Lenardd, Could you please provide the code of the CustomSettingsProvider class? I have tested similar code and did not meet the same issue. The code I tested can be download from http://msdn.microsoft.com/en-us/library/ms181001(VS.80).aspx. Regards, Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Monday, September 07, 2009 10:54 AM |
Hi Lenardd,
Could you please provide the code of the CustomSettingsProvider class?
Regards, Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Tuesday, September 08, 2009 9:44 AM |
Hi,
We are changing the issue type to “General Discussion�because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to “Question�by opening the Options list at the top of the post window, and changing the type. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Friday, September 11, 2009 6:28 AM |
Alan, my apologies for not responding sooner. My code is below:
public class CustomSettingsProvider : SettingsProvider
{
const string SETTINGSROOT = "userSettings";
private XmlDocument settingsXML = null;
public override void Initialize(string name, NameValueCollection collection)
{
base.Initialize("CustomSettingsProvider", collection);
settingsXML = new XmlDocument();
try
{
settingsXML.Load(Path.Combine(GetAppSettingsPath(), GetAppSettingsFilename()));
}
catch
{
// Create new document
XmlDeclaration dec = settingsXML.CreateXmlDeclaration("1.0", "utf-8", string.Empty);
settingsXML.AppendChild(dec);
XmlNode nodeRoot = default(XmlNode);
nodeRoot = settingsXML.CreateNode(XmlNodeType.Element, SETTINGSROOT, "");
settingsXML.AppendChild(nodeRoot);
}
}
public override string ApplicationName
{
get
{
return Assembly.GetExecutingAssembly().GetName().Name;
}
set { }
}
public virtual string GetAppSettingsPath()
{
return Path.GetDirectoryName(Application.CommonAppDataPath);
}
public virtual string GetAppSettingsFilename()
{
// Used to determine the filename to store the settings
return ApplicationName + ".settings";
}
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
{
foreach (SettingsPropertyValue propertyValue in collection)
{
SetValue(propertyValue);
}
settingsXML.Save(Path.Combine(GetAppSettingsPath(), GetAppSettingsFilename()));
// HACK - Manually delete the application's "version directory". Don't need it.
if (Directory.Exists(Application.CommonAppDataPath))
{
Directory.Delete(Application.CommonAppDataPath);
}
}
public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection collection)
{
// Create new collection of values
SettingsPropertyValueCollection values = new SettingsPropertyValueCollection();
// Iterate through the settings to be retrieved
foreach (SettingsProperty setting in collection)
{
SettingsPropertyValue value = new SettingsPropertyValue(setting);
value.IsDirty = false;
value.SerializedValue = GetValue(setting);
values.Add(value);
}
return values;
}
private string GetValue(SettingsProperty setting)
{
string ret = string.Empty;
try
{
ret = settingsXML.SelectSingleNode(SETTINGSROOT + "/" + setting.Name).InnerText;
}
catch
{
if ((setting.DefaultValue != null))
{
ret = setting.DefaultValue.ToString();
}
else
{
ret = string.Empty;
}
}
return ret;
}
private void SetValue(SettingsPropertyValue propertyValue)
{
XmlElement settingNode = default(XmlElement);
try
{
settingNode = (XmlElement)settingsXML.SelectSingleNode(SETTINGSROOT + "/" + propertyValue.Name);
settingNode.InnerText = propertyValue.SerializedValue.ToString();
}
catch
{
if (IsUserScopedSetting(propertyValue.Property))
{
settingNode = settingsXML.CreateElement(propertyValue.Name);
settingNode.InnerText = propertyValue.SerializedValue.ToString();
settingsXML.SelectSingleNode(SETTINGSROOT).AppendChild(settingNode);
}
}
}
private bool IsUserScopedSetting(SettingsProperty property)
{
// Determine if the setting is marked as User
foreach (DictionaryEntry d in property.Attributes)
{
Attribute a = (Attribute)d.Value;
if (a is System.Configuration.UserScopedSettingAttribute)
{
return true;
}
}
return false;
}
} |
| Lenardd Saturday, September 12, 2009 7:35 AM |
Hi Lenardd,
Sorry for the late reply. I have created a test project and test your code, but I did not meet the same issue. Could you please provide more information or the detail steps to reproduce the issue. You can also create a small project in which the issue can be reproduced and email it to me: alala666888@hotmail.com.
Regards, Aland Li Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread. |
| Aland Li Monday, September 14, 2009 5:18 AM |
It is because you use Application.CommonAppDataPath. Reading the property will cause the directory to be created as a side-effect. Just remove the code that reads and deletes the directory and it shouldn't be created anymore. Assuming there is no other code in your app that uses the property.
Hans Passant.- Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 2:17 AM
-
|
| nobugz Monday, September 14, 2009 12:37 PM |
Thanks nobugz. I guess I will put up with it since I need to know the "CommonAppDataPath". --Lenard |
| Lenardd Tuesday, September 15, 2009 6:46 PM |