简体   繁体   中英

How to change app.config file in window application c#

I have a window application and i have used the Email Functionality in that and in that the Email settings are in the app.config file.and its like below

<system.net>
<mailSettings>
  <smtp from="email@gmail.com">
    <network host="smtp.gmail.com" port="587" userName="username" password="password" enableSsl="true" />

  </smtp>
</mailSettings>
</system.net>

i want to change using the coding of above section..i have to change through coding in smtp and network section in config file.

i think you are asking how to do this programmatically?

there is an answer here:

Sending email through Gmail SMTP server with C#

also, a good example on the msdn site:

http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

let me know if this is not what you were asking..

Edit: to show answer once question was clear:

How can I read/write app.config settings at runtime without using user settings?

good luck

I found the Solution ....

 FileInfo objFileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location + ".config");
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(objFileInfo.FullName);
            XmlElement objElement = objXmlDoc.DocumentElement;
            XmlNode objAppSettingNode = objElement.SelectSingleNode("appSettings");

            foreach (XmlNode appSetting in objAppSettingNode)
            {
                if (appSetting.Name.Equals("add"))
                {
                    appSetting.Attributes["value"].Value = txtEmailAddress.Text.Trim();
                }

            }

            XmlNode objMailSettingNode = objXmlDoc.DocumentElement.SelectSingleNode("system.net/mailSettings");
            if (objMailSettingNode != null)
            {
                foreach (XmlNode childNode in objMailSettingNode)
                {
                    if (childNode.Name.ToLower().Equals("smtp"))
                    {
                        childNode.Attributes["from"].Value = txtEmailAddress.Text.Trim();
                        foreach (XmlNode networkNode in childNode)
                        {
                            if (networkNode.Name.ToLower().Equals("network"))
                            {
                                networkNode.Attributes["host"].Value = txtSmtp.Text.Trim();
                                networkNode.Attributes["userName"].Value = txtEmailAddress.Text.Split('@')[0].Trim();
                                networkNode.Attributes["password"].Value = txtPassword.Text.Trim();
                                networkNode.Attributes["port"].Value = txtPort.Text.Trim();
                            }
                        }
                    }
                }
            }

            objXmlDoc.Save(objFileInfo.FullName);
            lblErrormsg.Text = string.Empty;
            this.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM