• 14
• 12
• 9
• 10
• 13

# Application Settings in C Sharp

This topic is 3700 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have set up a form that has 2 options in it. 1) mtpServer 2) SmtpPort How can I read them in and change them and then save them back to the Application Settings file. PLEASE make it as simple as possible so that I can under stand.

##### Share on other sites
Use the Settings class.

Using Settings in C#

##### Share on other sites
Quote:
 Original post by Justin RebuiltUse the Settings class.Using Settings in C#

That a Page I all ready read threw and didn't under stand a thing.

##### Share on other sites
I'm not sure how much simpler it can get than that page.

I'm guessing your smtpServer and smtpPort variables are being obtained from TextBox components? You can get the value of them by using the Text or Value property, depending on what type of component they are.

After you get the values, simply set the settings values and save them using the Settings class.

Properties.Settings.Default.smtpServer = TextBox1.Value
Properties.Settings.Default.smtpPort = TextBox2.Value
Properties.Settings.Default.Save();

Obviously you change the variable names to match your own.

##### Share on other sites
Quote:
 Original post by Justin RebuiltI'm not sure how much simpler it can get than that page.I'm guessing your smtpServer and smtpPort variables are being obtained from TextBox components? You can get the value of them by using the Text or Value property, depending on what type of component they are.After you get the values, simply set the settings values and save them using the Settings class.Properties.Settings.Default.smtpServer = TextBox1.ValueProperties.Settings.Default.smtpPort = TextBox2.ValueProperties.Settings.Default.Save();Obviously you change the variable names to match your own.

Well This is what I have. and well that part I do under stand.
Heres my settings. This in what I have for the save.

Properties.Settings.Settings.SmtpServer = TextBox1.Value1
Properties.Settings.Settings.SmtpPort = TextBox2.Value2
Properties.Settings.Settings.Save();

What I'm trying to point out is that how do I load the current setting in to be displayed on the screen or form. I have form3 that displays or should display the current setting and then the user has an option of changing one or the other or even both.

I Have TextBox1.Text = SmtpServer; // For the SmtpServer Address
and I have TextBox2.Text = SmtpPort; // For the Port number
I just want to display the current setting.
And it tells me that they are not definded.

Then I can use

if (TextBox1.Text <> "")
{
Properties.Settings.Settings.SmtpServer = TextBox1.Value1
}
This will tell if the Value1 is blank or not and if the field is not bank then it will update it.
and then I can save it.

##### Share on other sites
The compiler is telling you what's wrong. You haven't defined SmtpServer and SmtpPort. If you're trying to retrieve them from settings:

Example:
int SmtpPort = Properties.Settings.Default.SmtpPort;

##### Share on other sites
Quote:
 Original post by Justin RebuiltThe compiler is telling you what's wrong. You haven't defined SmtpServer and SmtpPort. If you're trying to retrieve them from settings:Example:int SmtpPort = Properties.Settings.Default.SmtpPort;

Thank You I was missing the Default part. One more thing

On this line

Properties.Settings.Default.SmtpPort = Value2.Text;

Witch is an INT for the port being used. I get this error. and Yes I know what this error is.

Error 1 Cannot implicitly convert type 'string' to 'int'

How Can I process it as a string.

##### Share on other sites
Use something like

if (!int.TryParse(Value2.Text, out Properties.Settings.Default.SmtpPort)){  // Conversion failed - do something sensible like warn the user.}

##### Share on other sites
Quote:
 Original post by TheUnbelieverUse something like if (!int.TryParse(Value2.Text, out Properties.Settings.Default.SmtpPort)){ // Conversion failed - do something sensible like warn the user.}

Thank You now I have a new error
Error 1 A property or indexer may not be passed as an out or ref

So Heres my code. All of it.

public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
// Load Current setting for Smtp Settings
String SmtpServer = Properties.Settings.Default.SmtpServer;
int SmtpPort = Properties.Settings.Default.SmtpPort;

label2.Text = SmtpServer;
//label3.Text = SmtpPort; // I need to display the INT in the label3

}

private void button2_Click(object sender, EventArgs e)
{
// User Aborts Save
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
// User Updates and saves New Setting;;

if (Value1.Text != "")
{

Properties.Settings.Default.SmtpServer = Value1.Text;
}
if (Value2.Text != "")
{
if (!int.TryParse(Value2.Text, out Properties.Settings.Default.SmtpPort))
{
// Conversion failed - do something sensible like warn the user.
// I put a Message Box here
}

}

// Save The enties if any
Properties.Settings.Default.Save();
this.Close();

}
}

int smtpPort = Properties.Settings.Default.SmtpPort;if (!int.TryParse(Value2.Text, out smtpPort)){  // Conversion failed - do something sensible like warn the user.}Properties.Settings.Default.SmtpPort = smtpPort;