[英]How do I properly create during runtime and read the properties that are stored in the config file that saved during a previous session?
我在這里使用此代碼在運行時創建用戶范圍的設置:
System.Configuration.SettingsProperty userScopedProperty =
new System.Configuration.SettingsProperty("New Setting");
userScopedProperty.DefaultValue = "This setting default value";
userScopedProperty.IsReadOnly = false;
userScopedProperty.PropertyType = typeof(string);
userScopedProperty.Provider =
Properties.Settings.Default.Providers["LocalFileSettingsProvider"];
userScopedProperty.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute),
new System.Configuration.UserScopedSettingAttribute());
Properties.Settings.Default.Properties.Add(userScopedProperty);
Properties.Settings.Default["New Setting"] = "value changed to this";
Properties.Settings.Default.Save();
Properties.Settings.Default.Reload();
由於某種原因,它有時只能工作,並且會在 user.config 文件的 userSettings 部分中創建設置。 在同一個 session 中執行時,這有效:
Properties.Settings.Default["New Setting"]
但是一旦我關閉應用程序並再次啟動它,那行代碼就會給我一個System.Configuration.SettingsPropertyNotFoundException
。 我嘗試在嘗試讀取設置之前添加Properties.Settings.Default.Reload()
,但這似乎也不起作用。
該項目這部分的目標是能夠在運行時創建用戶設置、關閉程序,並且當您再次啟動該程序時您可以查看這些設置。 您還應該能夠隨時更改它們。 我在 Visual Studio 中以調試模式運行該程序,不確定是否需要該信息。
所以我的問題是:如何在運行時正確創建並讀取存儲在我在之前的 session 中保存的 user.config 文件中的屬性? 有一個更好的方法嗎?
我通常將類似的設置保存在IsolatedStorage的 XML 文件中。 我創建了一個ProgramSettings
class,我為 XML 序列化注釋(使用XmlRootAttribute
、 XmlElementAttribute
,有時使用XmlArrayAttribute
和XmlArrayItemAttribute
),然后我使用這個 class 來讀取和寫入 XML 設置文件:
public static class PersistedSettings<T> where T : class, new()
{
public static T LoadSettings(string applicationName, ISettingsErrorReporter errorReporter)
{
var filename = GetFileName(applicationName);
try
{
using (var isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null))
{
if (!isoStore.FileExists(filename))
{
return new T();
}
//otherwise
using (var settingsStream = isoStore.OpenFile(filename, FileMode.Open, FileAccess.Read))
{
var serializer = new XmlSerializer(typeof(T));
var settings = (T)serializer.Deserialize(settingsStream);
settingsStream.Close();
return settings;
}
}
}
catch (IOException ioException)
{
errorReporter.WriteError($"IO Exception:{Environment.NewLine}{ioException.Message}");
}
catch (Exception ex)
{
errorReporter.WriteError($"Exception ({ex.GetType().Name}):{Environment.NewLine}{ex.Message}");
}
//no matter what kind of exception,
return new T();
}
public static void SaveSettings(string applicationName, T settings, ISettingsErrorReporter errorReporter)
{
try
{
using (var isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null))
{
using (var settingsStream = isoStore.CreateFile(GetFileName(applicationName)))
{
var serializer = new XmlSerializer(typeof(T));
serializer.Serialize(settingsStream, settings);
//let's be safe and use both suspenders and a belt (Flush, Close, Dispose)
settingsStream.Flush();
settingsStream.Close();
}
}
}
catch (IOException ioException)
{
errorReporter.WriteError($"I/O Exception:{Environment.NewLine}{ioException.Message}");
}
catch (Exception ex)
{
errorReporter.WriteError($"Exception ({ex.GetType().Name}):{Environment.NewLine}{ex.Message}");
}
}
private static string GetFileName(string applicationName)
{
return applicationName + ".xml";
}
}
我通常在簡單的 WinForms 應用程序中使用它。 我讓我的表格 class 實現這個接口:
public interface ISettingsErrorReporter
{
void WriteError(string message);
}
(通常通過彈出消息框的方式)
然后我在 FormLoad 事件中調用LoadSettings
,在窗體關閉事件中調用SaveSettings
。
這不是您要的,但它可能會滿足您的需求
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.