Connection string in C # wpf

Mar 1 2017 8:30 PM

Hi, my name is Stalin and I am writing to you because I have a problem.

When I install the application on the PC, it creates a document called App.config, dynamically. then I run the installed program and it does not recognize the created document (App.config) so I can not connect to my database, but when it is in Visual Studio works well.

I'm working with WPF, AppServer on my localhost.

Very lot of thanks, I am waiting for your answer

NOTE: I speak Spanish and I used a translator

With this I created the App.config document dynamically

<code>

string server = txtServidor.Text;

string puerto = txtPuerto.Text;

string baseDatos = txtBaseDatos.Text;

string usuario = txtUsuario.Text;

string clave = txtClave.Password;

string conexString = "Server=" + server + ";Port=" + puerto + ";Database=" + baseDatos + ";Uid=" + usuario + ";password=" + clave + ";";

string fechaInstalacion = DateTime.Now.ToString("dd/MM/yyyy");

DateTime s = DateTime.Today.Date.AddDays(15);

string fechaEnCaducar = s.ToString("dd/MM/yyyy");

XDocument xml = new XDocument(new XDeclaration("1.0", "utf-8", null));

/*Conexion a base de datos*/

XElement raiz = new XElement("configuration");

xml.Add(raiz);

//startup

XElement startup = new XElement("startup");

raiz.Add(startup);

XElement supportedRuntime = new XElement("supportedRuntime",

new XAttribute("version", "v4.0"),

new XAttribute("sku", ".NETFramework,Version=v4.5"));

startup.Add(supportedRuntime);

//connectionStrings

XElement connectionStrings = new XElement("connectionStrings");

raiz.Add(connectionStrings);

XElement add = new XElement("add",

new XAttribute("name", "ConsStringLocal"),

new XAttribute("connectionString", conexString));

XElement addFechaInstalacion = new XElement("add",

new XAttribute("name", "FechaInstalacion"),

new XAttribute("connectionString", fechaInstalacion));

/*XElement addFechaEnCaducar = new XElement("add",

new XAttribute("name", "FechaEnCaducar"),

new XAttribute("connectionString", fechaEnCaducar));*/

XElement addInstalacion = new XElement("add",

new XAttribute("name", "Instalacion"),

new XAttribute("connectionString", 1));

connectionStrings.Add(add);

connectionStrings.Add(addFechaInstalacion);

/*connectionStrings.Add(addFechaEnCaducar);*/

connectionStrings.Add(addInstalacion);

// Guardar el xml mediante el cuadro de dialogo

Microsoft.Win32.SaveFileDialog guardar = new Microsoft.Win32.SaveFileDialog();

guardar.Filter = "Archivo xml | *.config";

// Abrir el cuadro de dialogo para guardar el xml

if (guardar.ShowDialog() == true)

{

try

{

// guardar el nuevo documento xml

xml.Save(guardar.FileName);

MessageBox.Show("Documento de conexion creado con exito");

}

catch (Exception ex)

{

// manejar la excepcion

MessageBox.Show("Error en guardar :: " + ex.Message);

}

}

</code>

 
 
 

This is the result of the created file

 
 
 

When I am working in Visual Studio the document is modified without problems, once the installer has been created and is already installed on a PC, the document that is modified does not reconfigure it, as if it were never modified.

I do not know if that will be Visual Studio error that once created the installed can not be modified or added more files.


Answers (3)