Winform project installer in vs 2008 does not recognize the app.config

Dear I have a problem, I have a project in vs2008 that works perfectly, I create the installer and everything is fine, the drawback is when installing it on another machine does not work, I put a log to see where the error comes out, and every time the program tries to access the app.config skips the exception " object reference not set as instance...", and that happens every time I try to access the app keys.config. When installing in the directory from which it is installed the application if the app appears.config and an app.DLL.config, but as I tell you do not recognize it does not access or do not know what is the dilemma after installing it that you can not access the app.config.

This is the method where the error happens:

public OleDbConnection CConec()
        {
            FileStream stream = new FileStream("c:\\xxx\\" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString() + ".txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter writer = new StreamWriter(stream);
            writer.WriteLine("First Line");
            // Escribimos fecha y hora del instante
            writer.WriteLine("Second Line and time " + DateTime.Now);
            // Escribimos User y Hostname del equipo
            System.Security.Principal.WindowsIdentity user = System.Security.Principal.WindowsIdentity.GetCurrent();
            writer.WriteLine("User :  " + user.Name);
            writer.WriteLine("Hostname : " + Environment.MachineName);
            try
            {

                writer.WriteLine("000");
                p_strSVRSQL = ConfigurationSettings.AppSettings["SVRSQL"].ToString();
                writer.WriteLine("111");
                p_strUSRSQL = ConfigurationSettings.AppSettings["USERSQL"].ToString();
                writer.WriteLine("222");
                p_strPWDSQL = ConfigurationSettings.AppSettings["PWDSQL"].ToString();
                writer.WriteLine("333");
                //strCnn = "Provider=SQLOLEDB.1;Server=" + p_strSVRSQL + ";Database=MICDB;Uid=" + p_strUSRSQL + "; Pwd=" + p_strPWDSQL + ";";
                //strCnn = "Provider=SQLOLEDB.1;Server=" + Environment.MachineName + @";Database=MICDB;Uid=" + p_strUSRSQL + "; Pwd=" + p_strPWDSQL + ";";
                strCnn = "Provider=SQLOLEDB.1;Server=" + Environment.MachineName + @"\SQLEXPRESS;Database=xxx;Uid=xxx" + "" + "; Pwd=xxx" + "" + ";";
                //strCnn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=MICDB;Data Source=" + p_strSVRSQL;
                writer.WriteLine("444");
                oCon = new OleDbConnection(strCnn);
                writer.WriteLine("555");
                oCon.Open();
                writer.WriteLine("666");
                p_strStateConex = "Conectado A " + p_strSVRSQL;
                writer.WriteLine(p_strStateConex + " con " + strCnn);

                writer.Close();
            }
            catch (Exception ex)
            {
                writer.WriteLine(ex.Message);
                writer.Close();
            }
            return oCon;
        }

As you can see there is code (to increase it to see the error) to save line by line what happens, and the content of the file is:

First Line Second Line and time 19/05/2016 18: 35: 50 User : xxx-90B2E1 \ Administrator Hostname: xxx-90B2E1 000 Object reference not set as an instance of an object

And as I said before, in development it works perfectly, once installed on a client machine is the problem.

Note. 1. All the code to write to a text file increase it to see where and what happens in the client, i.e. to see until which line is executed and what is the exception. 2. The machine where development is WXP with VS2008, the machine where I install on a W7.

Greetings.

 0
Author: jasilva, 2016-05-19

1 answers

ConfigurationSettings is what is not working for you. Suppose your app.config has the following format:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- ... -->
    <add key="SVRSQL" value="blablabla"/>
    <!-- ... -->
  </appSettings>
</configuration>

The Class ConfigurationSettings has been marked as deprecated. You could use the following options:

// Utilizando el ConfigurationManager. Debes agregar **using System.Configuration**
ConfigurationManager.AppSettings["SVRSQL"];
// Otra forma más complicada, pero sirve
var lector = new System.Configuration.AppSettingsReader();
string valor = lector.GetValue("SVRSQL",typeof(string)).ToString();
 1
Author: Christian Amado, 2016-05-20 15:36:27