我有textbox我在其中select path的.txt,在它被保存和编码的数据SqlConnection:
textbox
select
path
.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中以调试模式测试此方法时,将看到仅AppConfig.vshost.exe.config在传递值的情况下进行更新。
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"); }