How to rescue the App.config connection string from another project?

Asked

Viewed 727 times

0

I have these connection strings on App.config of another project:

<connectionStrings>
   <add name="ConexaoTeste1" 
    connectionString="RGF0YSBTb3VyY2U9JCQkXFNRTEVYUFJFU1M7SW5pdGlhbCBDYXRhbG9nPXRlc3RlOw==" 
    providerName="System.Data.SqlClient"/>

   <add name="ConexaoTeste2" 
    connectionString="RGF0YSBTb3VyY2U9dGVzdGU7UGVyc2lzdCBTZWN1cml0eSBJbmZvPVRydWU7" 
    providerName="System.Data.OracleClient"/>
</connectionStrings>

This other project is not on my computer. It’s on the network.

How the value of ConexaoTeste2 in the my project?

2 answers

1

Add reference System.Configuration in your project and make:

string conexao = 
    System.Configuration.ConfigurationManager.
    ConnectionStrings["ConexaoTeste2"].ConnectionString;

About the fact that the App.config remote: Here has a good reason not to do so, and has also some alternatives.

  • 1

    Young man, he said a few minutes ago that’s not what you need.

  • As I asked the question, the config is remote, has a whole question of access permission, the problem is a little more hairy.

  • Crazy. When I went to answer, there was no answer, @jbueno . It wasn’t because I wanted to.

  • @ROBERTOALBINOJUNIOR I had not seen. I edited the answer.

  • 1

    @Igorventurelli I had erased mine, I just restored it so you could see the comments.

  • @jbueno edited the question. It is under analysis. I left more direct and explicit about the fact of being remote.

Show 1 more comment

0

I was able to solve the problem with the code below, ignoring the file as config and treating it as a normal xml, I went through the document, I found the node that needed to change and it worked:

XmlDocument xml = new XmlDocument();
xml.Load("\\server\\configs\\teste.exe.config");
xml.SelectNodes("configuration/connectionStrings/add").Item(1).Attributes["connectionString"].Value = "foi";
xml.Save("\\server\\configs\\teste.exe.config");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.