摘要:app Config 寫入 (XML)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Xml;
using System.Web.Configuration;
using System.Text.RegularExpressions;
namespace ML.Web.UserAdmin
{
public partial class ML03_02_MailSettings : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadConfig();
}
}
private void LoadConfig()
{
XmlDocument doc = LoadConfigDocument();
txtSubject.Text = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='Subject']/value").InnerText.ToString().Trim();
txtMailTo.Text = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='MailTo']/value").InnerText.ToString().Trim();
txtMailCC.Text = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='MailCC']/value").InnerText.ToString().Trim();
txtMailFrom.Text = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='MailFrom']/value").InnerText.ToString().Trim();
txtDomain.Text = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='Domain']/value").InnerText.ToString().Trim();
txtNetworkAccount.Text = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='NetworkAccount']/value").InnerText.ToString().Trim();
txtNetworkPassword.Text = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='NetworkPassword']/value").InnerText.ToString().Trim();
txtSmtpClient.Text = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='SmtpClient']/value").InnerText.ToString().Trim();
}
private XmlDocument LoadConfigDocument()
{
XmlDocument doc = null;
try
{
doc = new XmlDocument();
doc.Load(GetConfigFilePath());
return doc;
}
catch (System.IO.FileNotFoundException e)
{
throw new Exception("No configuration file found.", e);
return null;
}
catch (Exception ex)
{
return null;
}
}
private string GetConfigFilePath()
{
return Server.MapPath("~\\MLSendReportJob\\MLSendReportJob.exe.config");
}
protected void btnAdvanced_Click(object sender, EventArgs e)
{
ASPxPopupControl1.ShowOnPageLoad = true;
}
protected void btnSave1_Click(object sender, EventArgs e)
{
string notes = "修改ML Mail設定檔";
//載入設定
XmlDocument doc = LoadConfigDocument();
XmlNode subject = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='Subject']/value");
XmlNode mailto = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='MailTo']/value");
XmlNode mailcc = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='MailCC']/value");
//紀錄LOG
notes += " / Subject [修改前:" + subject.InnerText + "][修改後:" + txtSubject.Text + "]";
notes += " / MailTO [修改前:" + mailto.InnerText + "][修改後:" +RefomatMailList( txtMailTo.Text) + "]";
notes += " / MailCC [修改前:" + mailcc.InnerText + "][修改後:" +RefomatMailList( txtMailCC.Text) + "]";
WriteLog(notes);
//寫入Config
subject.InnerText = txtSubject.Text;
mailto.InnerText =RefomatMailList( txtMailTo.Text);
mailcc.InnerText =RefomatMailList (txtMailCC.Text);
//儲存
doc.Save(GetConfigFilePath());
ScriptManager.RegisterClientScriptBlock (this.Page,typeof(Page), "alert", " alert('儲存成功');",true);
//重新載入
LoadConfig();
ASPxGridView1.DataBind();
}
protected void btnSave2_Click(object sender, EventArgs e)
{
string notes = "修改ML Mail進階設定檔";
XmlDocument doc = LoadConfigDocument();
XmlNode mailform = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='MailFrom']/value");
XmlNode domain = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='Domain']/value");
XmlNode netacc = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='NetworkAccount']/value");
XmlNode netpwd = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='NetworkPassword']/value");
XmlNode smtp = doc.SelectSingleNode("configuration/applicationSettings/MLSendReportJob.Settings/setting[@name='SmtpClient']/value");
//紀錄LOG
notes += " / MailFrom [修改前:" + mailform.InnerText + "][修改後:" +RefomatMailList( txtMailFrom.Text) + "]";
notes += " / Domain [修改前:" + domain.InnerText + "][修改後:" + txtDomain.Text + "]";
notes += " / Network Account [修改前:" + netacc.InnerText + "][修改後:" + txtNetworkAccount.Text + "]";
//安全問題,pwd 不記LOG
//notes += " / Network Password [修改前:" + netpwd.InnerText + "][修改後:" + txtNetworkPassword.Text + "]";
notes += " / SmtpClient [修改前:" + smtp.InnerText + "][修改後:" + txtSmtpClient.Text + "]";
WriteLog(notes);
//寫入Config
mailform.InnerText = RefomatMailList(txtMailFrom.Text);
domain.InnerText = txtDomain.Text;
netacc.InnerText = txtNetworkAccount.Text;
netpwd.InnerText = txtNetworkPassword.Text;
smtp.InnerText = txtSmtpClient.Text;
//儲存
doc.Save(GetConfigFilePath());
ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), "alert", " alert('儲存成功');", true);
//重新載入
LoadConfig();
ASPxGridView1.DataBind();
}
private string RefomatMailList(string input)
{
string output="";
string[] mailList = input.Split(';').Where(p => p.ToString() != string.Empty).ToArray();
output = string.Join(";", mailList);
return output;
}
private void WriteLog( string notes)
{
Coretronic.BI.ML.Log.WriteLog(Request.Url.Segments[Request.Url.Segments.Length - 1], "MailSettingConfig", notes, Page.User.Identity.Name);
}
}
}