在 appSettings 中存储值
Storing values in appSettings
我试图在我的配置文件中存储一个用户 ID,然后出于多种原因我想在整个程序中访问它。我在配置文件中有 appSettings
部分,就像这样;
<appSettings>
<add key="userID" value="1" />
</appSettings>
然后我用一种方法设置值;
private void hrButton_Click(object sender, RoutedEventArgs e)
{
DataAccessor da = new DataAccessor();
DataRowView dataRow = (DataRowView)dataGrid.SelectedItem;
// Open App.Config of executable
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("userID", dataRow.Row.ItemArray[0].ToString());
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
da.SetUserDetails();
NavigationService.Navigate(new Uri(@"View/HRSystem/HRSystem.xaml", UriKind.Relative));
}
在尝试在另一个中访问和使用它之前;
public List<string> SetUserDetails()
{
string userID = ConfigurationManager.AppSettings.Get("userID");
MessageBox.Show("userID: "+userID);
string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
using (OleDbConnection dbfCon = new OleDbConnection(constr))
{
try
{
dbfCon.Open();
OleDbDataReader myReader;
OleDbCommand sqlCmd = new OleDbCommand("SELECT em_pplid FROM employs WHERE em_pplid = "+ userID + "", dbfCon);
myReader = sqlCmd.ExecuteReader();
List<string> listUser = new List<string>();
while (myReader.Read())
{
listUser.Add(myReader[0].ToString());
return listUser;
}
}
catch (MySqlException)
{
throw;
}
}
return null;
}
但是当我在消息框中显示userID
时它仍然设置为1。我以前没有使用过appSettings
部分,为什么它总是1?我是 运行 Visual Studio 2015,C# WPF。
尝试
config.AppSettings.Settings.Remove('userID');
config.AppSettings.Settings.Add('userID', dataRow.Row.ItemArray[0].ToString());
但是您不应该将用户标识存储在配置文件中。您应该将其保存在会话或 Cookie
在 config.AppSettings.Settings.Add();
正上方的行中添加 config.AppSettings.Settings.Remove("userID");
但是保存此类信息的正确方法是使用 Settings File
设置:
Properties.Settings.Default.userID = dataRow.Row.ItemArray[0].ToString();
Properties.Settings.Default.Save();
检索:
var userId = Properties.Settings.Default.userID;
我试图在我的配置文件中存储一个用户 ID,然后出于多种原因我想在整个程序中访问它。我在配置文件中有 appSettings
部分,就像这样;
<appSettings>
<add key="userID" value="1" />
</appSettings>
然后我用一种方法设置值;
private void hrButton_Click(object sender, RoutedEventArgs e)
{
DataAccessor da = new DataAccessor();
DataRowView dataRow = (DataRowView)dataGrid.SelectedItem;
// Open App.Config of executable
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("userID", dataRow.Row.ItemArray[0].ToString());
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
da.SetUserDetails();
NavigationService.Navigate(new Uri(@"View/HRSystem/HRSystem.xaml", UriKind.Relative));
}
在尝试在另一个中访问和使用它之前;
public List<string> SetUserDetails()
{
string userID = ConfigurationManager.AppSettings.Get("userID");
MessageBox.Show("userID: "+userID);
string constr = ConfigurationManager.ConnectionStrings["dbfString"].ConnectionString;
using (OleDbConnection dbfCon = new OleDbConnection(constr))
{
try
{
dbfCon.Open();
OleDbDataReader myReader;
OleDbCommand sqlCmd = new OleDbCommand("SELECT em_pplid FROM employs WHERE em_pplid = "+ userID + "", dbfCon);
myReader = sqlCmd.ExecuteReader();
List<string> listUser = new List<string>();
while (myReader.Read())
{
listUser.Add(myReader[0].ToString());
return listUser;
}
}
catch (MySqlException)
{
throw;
}
}
return null;
}
但是当我在消息框中显示userID
时它仍然设置为1。我以前没有使用过appSettings
部分,为什么它总是1?我是 运行 Visual Studio 2015,C# WPF。
尝试
config.AppSettings.Settings.Remove('userID');
config.AppSettings.Settings.Add('userID', dataRow.Row.ItemArray[0].ToString());
但是您不应该将用户标识存储在配置文件中。您应该将其保存在会话或 Cookie
在 config.AppSettings.Settings.Add();
config.AppSettings.Settings.Remove("userID");
但是保存此类信息的正确方法是使用 Settings File
设置:
Properties.Settings.Default.userID = dataRow.Row.ItemArray[0].ToString();
Properties.Settings.Default.Save();
检索:
var userId = Properties.Settings.Default.userID;