How to make a string in Appsettings and use it later

Asked

Viewed 2,857 times

2

How can I make a string in Appsettings, and then use it, for example, the string in Appsettings is this:

string example = "example"

And take the value that’s in the Appsettings string and put it in a textbox, how can I do this?

  • 1

    string test = Configurationmanager.Appsettings["example"] ?

  • 1

    In the app.config: <appSettings> <add key="example " value="example"/> </appSettings>

3 answers

2


Only use ConfigurationManager.AppSettings[chave].

using System.Configuration;

// outras coisas 

public static void AlgumaFuncao()
{
    var valor = ConfigurationManager.AppSettings["exemplo"];        
    meuTextBox.Text = valor.ToString();
}

Remembering that the config of the application needs to look like this

<configuration>
  <appSettings>
    <add key="exemplo" value="Alguma string de exemplo"/>
  </appSettings>
</configuration>

0

In his app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="exemplo" value="exemplo"/>
  </appSettings>
</configuration>

In code C#:

string exemplo = System.Configuration.ConfigurationManager.AppSettings["exemplo"];

Remember to add refrence to System.Configuration

0

To put in a txtbox you must do this:

txtbox.text = Properties.Settings.Default.teste;

To save is so:

Properties.Settings.Default.teste = txtbox.Text;
Properties.Settings.Default.Save();
Properties.Settings.Default.Upgrade();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.