永久用户设置

时间:2018-09-10 09:10:42

标签: c# persistence settings

我试图将用户设置保存在C#中,并在下一个会话中重复使用。

在网上,人们发现这样的小费: How to save user settings programatically?

我的项目中有一个默认设置的.setting文件。

当用户更改设置时,我呼叫save,并在%APPDATA%中显示带有用户设置的文件。 看起来像这样:

private void SetSetting(string prop, Object val)
{
    try
    {
        string strval = String.Format("G{0}_{1}", Group_Id, prop);
        Properties.Last.Default[strval] = val;
    }
    catch
    {
        System.Configuration.SettingsProperty property = new System.Configuration.SettingsProperty(String.Format("G{0}_{1}", Group_Id, prop));
        property.PropertyType = Properties.Def.Default[String.Format("G0_{1}", Group_Id, prop)].GetType();
        property.DefaultValue = Properties.Def.Default[String.Format("G0_{1}", Group_Id, prop)];
        property.PropertyType = val.GetType();
        property.Provider = Properties.Last.Default.Providers["LocalFileSettingsProvider"];
        property.Attributes.Add(typeof(System.Configuration.UserScopedSettingAttribute), new System.Configuration.UserScopedSettingAttribute());
        Properties.Last.Default.Properties.Add(property);
        Properties.Last.Default.Reload();
        string strval = String.Format("G{0}_{1}", Group_Id, prop);
        Properties.Last.Default[strval] = val;
    }
    Properties.Last.Default.Save();
}

如您所见,我的设置实例为Last,如“上一次会话的设置”,默认为Def

基本上,用户可以在应用程序中随意添加和删除组。 Def保留新组的默认配置。 Last应该包含上一个用户会话中的组数以及每个组的属性。因此,使用Group_Id进行格式化,依此类推。

所以,正如我所说。在运行时,它很好。用户设置已正确写入%APPDATA%中的文件中。

文件如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5629387e089" >
            <section name="Project1.Properties.Last" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5629387e089" " allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <Project1.Properties.Last>
            <setting name="G1_do_cut" serializeAs="String">
                <value>True</value>
            </setting>
        </Project1.Properties.Last>
    </userSettings>
</configuration>

因此,第1组的do_cut设置将另存为G1_do_cut

但是,一旦我重新启动应用程序,用户设置就会被忽略。 使用的代码是:

private Object GetSetting(string prop)
{
    try { return Properties.Last.Default[String.Format("G{0}_{1}", Group_Id, prop)]; }
    catch { return Properties.Def.Default[String.Format("G0_{1}", Group_Id, prop)]; }
}

,然后尝试抛出异常G1_do_cut is not found in settings,并且catch加载默认值。

在用户会话期间,GetSettings可以正常工作-如果在设置G1_do_cut之后尝试读取import tensorflow as tf import pandas as pd from matplotlib import pyplot from sklearn.preprocessing import MinMaxScaler from sklearn import linear_model from keras.models import Sequential from keras.layers import Dense #Standard neural network layer from keras.layers import LSTM from keras.layers import Activation from keras.layers import Dropout df = pd.read_csv('Testdaten_2_Test.csv',delimiter=';') feature_col_names=['LSDI','LZT1I', ..... ,'LZT5I'] predicted_class_names = ['LMDI'] x = df[feature_col_names].values y = df[predicted_class_names].values x_train_size = 6400 x_train, x_test = x[0:x_train_size], x[x_train_size:len(x)] y_train_size = 6400 y_train, y_test = y[0:y_train_size], y[y_train_size:len(y)] nb_model = linear_model.LinearRegression() nb_model.fit(X=x_train, y=y_train) nb_predict_train = nb_model.predict(x_test) from sklearn import metrics def scale(x, y): # fit scaler x_scaler = MinMaxScaler(feature_range=(-1, 1)) x_scaler = x_scaler.fit(x) x_scaled = x_scaler.transform(x) # fit scaler y_scaler = MinMaxScaler(feature_range=(-1, 1)) y_scaler = y_scaler.fit(y) y_scaled = y_scaler.transform(y) return x_scaler, y_scaler, x_scaled, y_scaled x_scaler, y_scaler, x_scaled, y_scaled = scale(x, y) x_train, x_test = x_scaled[0:x_train_size], x_scaled[x_train_size:len(x)] y_train, y_test = y_scaled[0:y_train_size], y_scaled[y_train_size:len(y)] x_train=x_train.reshape(x_train_size,1,18) y_train=y_train.reshape(y_train_size,1,1) model = Sequential() model.add(LSTM(10, return_sequences=True,batch_input_shape=(32,1,18))) model.add(LSTM(10,return_sequences=True)) model.add(LSTM(1,return_sequences=True, activation='linear')) model.compile(loss='mean_squared_error', optimizer='adam', metrics= ['accuracy']) model.fit(x_train, y_train, epochs=10,batch_size=32) score = model.evaluate(x_test, y_test,batch_size=32) predicted = model.predict(x_test) predicted = y_scaler.inverse_transform(predicted) predicted = [x if x > 0 else 0 for x in predicted] correct_values = y_scaler.inverse_transform(y_test) correct_values = [x if x > 0 else 0 for x in correct_values] print(nb_predict_train) ,则不会引发异常,并且我得到正确的值。

重新启动应用程序后,如何让C#重用设置?

0 个答案:

没有答案