gpt4 book ai didi

c# - 将数据从文本框存储到 appconfig

转载 作者:行者123 更新时间:2023-11-30 20:55:44 24 4
gpt4 key购买 nike

我有文本框,我在其中选择 path for .txt,其中保存和编码数据对于 SqlConnection:

 Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
textBox5.Text = string.Format("{0}", openFileDialog1.FileName) ;

// here I need some miracle to save default text for textBox5, appconfig maybe? according to which path was selected
nacti_spojeni();
}

但问题是用户每次想要连接到 SQL 数据库时都必须选择路径,我认为如果可能的话,可以将路径保存到应用程序配置中吗?我想到的其他事情是为文本框设置默认文本值。也许这是一个微不足道且毫无意义的问题。谢谢大家的宝贵时间。

最佳答案

您可以使用从 txt 框中传递的路径值更新配置文件,如下所示,

注意:当您在 visual studio 中以 Debug模式测试此方法时,您将仅看到 AppConfig.vshost.exe.config 将使用传递的值进行更新。

private static void UpdateConnectionString(string path)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["ConfigurationKeyForPath"].Value = path;

//Save only the modified section of the config
configuration.Save(ConfigurationSaveMode.Modified);

//Refresh the appSettings section to reflect updated configurations
ConfigurationManager.RefreshSection("appSettings");
}

关于c# - 将数据从文本框存储到 appconfig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18099910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com