noroozifar
چهارشنبه 02 دی 1388, 22:36 عصر
سلام توسط کدهای زیر می توانم در RunTime فایل app.config را تغییر بدهم
private void picOk_Click(object sender, EventArgs e)
{
try
{
//information for connection
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(AppDomain.CurrentDomain.BaseDirectory + @"..\..\App.config");
XmlNode appsetting = xmldoc.SelectSingleNode("configuration/connectionStrings");
foreach (XmlNode ChildNode in appsetting)
{
if (ChildNode.Attributes["name"].Value == "mydatabasename")
ChildNode.Attributes["connectionString"].Value = "Server=" + txtServer.Text + ";DataBase=" + txtDataBase.Text + ";User=" + txtUser.Text + ";Pwd=" + txtPasswordUser.Text;
if (ChildNode.Attributes["name"].Value == "Server")
ChildNode.Attributes["connectionString"].Value = txtServer.Text;
if (ChildNode.Attributes["name"].Value == "DataBase")
ChildNode.Attributes["connectionString"].Value = txtDataBase.Text;//name data base
if (ChildNode.Attributes["name"].Value == "User")
ChildNode.Attributes["connectionString"].Value = txtUser.Text;//name user id
if (ChildNode.Attributes["name"].Value == "Pwd")
ChildNode.Attributes["connectionString"].Value = txtPasswordUser.Text;//password bank
}
xmldoc.Save(AppDomain.CurrentDomain.BaseDirectory + @"..\..\App.config");
xmldoc.Save(AppDomain.CurrentDomain.SetupInformati on.ConfigurationFile);
//change conect
{
getconnectionstrings();
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(Configur ationUserLevel.None);
config.AppSettings.Settings.Add("NewConnectionString",Connect + " ");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
progressBar1.Maximum = 1000000;
progressBar1.Visible = true;
for (int j = 0; j < 1000000; j++)
progressBar1.Value += 1;
progressBar1.Visible = false;
i = 0;
this.Size = new Size(494, 216);//min
txtName.Focus();
}
catch (XmlException ml)
{
MessageBox.Show(ml.Message);
}
}
حالا توسط خود ویژوال یک SetUp برای برنامه ام ساخته ام ولی هنگام اجرا پیغام می دهد که نمی تواند فایل App.config را "c:\programfile\cmr\app.config" پیدا کنه چه کار باید کرد ?
private void picOk_Click(object sender, EventArgs e)
{
try
{
//information for connection
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(AppDomain.CurrentDomain.BaseDirectory + @"..\..\App.config");
XmlNode appsetting = xmldoc.SelectSingleNode("configuration/connectionStrings");
foreach (XmlNode ChildNode in appsetting)
{
if (ChildNode.Attributes["name"].Value == "mydatabasename")
ChildNode.Attributes["connectionString"].Value = "Server=" + txtServer.Text + ";DataBase=" + txtDataBase.Text + ";User=" + txtUser.Text + ";Pwd=" + txtPasswordUser.Text;
if (ChildNode.Attributes["name"].Value == "Server")
ChildNode.Attributes["connectionString"].Value = txtServer.Text;
if (ChildNode.Attributes["name"].Value == "DataBase")
ChildNode.Attributes["connectionString"].Value = txtDataBase.Text;//name data base
if (ChildNode.Attributes["name"].Value == "User")
ChildNode.Attributes["connectionString"].Value = txtUser.Text;//name user id
if (ChildNode.Attributes["name"].Value == "Pwd")
ChildNode.Attributes["connectionString"].Value = txtPasswordUser.Text;//password bank
}
xmldoc.Save(AppDomain.CurrentDomain.BaseDirectory + @"..\..\App.config");
xmldoc.Save(AppDomain.CurrentDomain.SetupInformati on.ConfigurationFile);
//change conect
{
getconnectionstrings();
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(Configur ationUserLevel.None);
config.AppSettings.Settings.Add("NewConnectionString",Connect + " ");
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
progressBar1.Maximum = 1000000;
progressBar1.Visible = true;
for (int j = 0; j < 1000000; j++)
progressBar1.Value += 1;
progressBar1.Visible = false;
i = 0;
this.Size = new Size(494, 216);//min
txtName.Focus();
}
catch (XmlException ml)
{
MessageBox.Show(ml.Message);
}
}
حالا توسط خود ویژوال یک SetUp برای برنامه ام ساخته ام ولی هنگام اجرا پیغام می دهد که نمی تواند فایل App.config را "c:\programfile\cmr\app.config" پیدا کنه چه کار باید کرد ?