Question

I am trying to get value of service in my application from app.config. I have to send it to the application which shows the URL. A web service which I am consuming in this aplication also using it so can not move it to appSettings.

I want to get this value 'http://192.168.4.22:82/Service.asmx' through c# code.

<applicationSettings>
    <SDHSServer.Properties.Settings>
      <setting name="DOServer_WebReference1_Service" serializeAs="String">
        <value>http://192.168.4.22:82/Service.asmx</value>
      </setting>
    </SDHSServer.Properties.Settings>
  </applicationSettings>
Was it helpful?

Solution

Not sure i get the question,

string s = SDHSServer.Properties.Settings.DOServer_WebReference1_Service;

will get you it

OTHER TIPS

If I understand you correctly you have two Visual Studio C# projects. The first (project A) has a setting you want to access in the second (project B). To do that you have to perform the following steps:

  • Add a reference from project B to project A

  • Change the access modifier of the settings i project A to public (default is internal)

    Visual Studio settings editor

  • Now you can access the setting in project B, in your case using the fully qualified name SDHSServer.Properties.Settings.Default.DOServer_WebReference1_Service

Note that in the settings editor you can set a value for the setting. This is the default value for the setting and this value is also stored in the App.config file for the project. However, you can override this value by providing another value in the App.config file for the application executing.

In this example, the App.config file for project A will contain the value for the setting which is http://192.168.4.22:82/Service.asmx. However, you can override this in the App.config file for project B to get another value. That is probably not what you want to do but you should be aware of this.

I use this code in a ASP.Net 4.0 site to pull section data out of the 'applicationsetting' section:

public sealed class SiteSupport {


    /// <summary>
    /// Retrieve specific section value from the web.config
    /// </summary>
    /// <param name="configSection">Main Web.config section</param>
    /// <param name="subSection">Child Section{One layer down}</param>
    /// <param name="innersection">Keyed on Section Name</param>
    /// <param name="propertyName">Element property name</param>
    /// <returns></returns>
    /// <example>string setting = NoordWorld.Common.Utilities.SiteSupport.RetrieveApplicationSetting("applicationSettings", "NoordWorld.ServiceSite.Properties.Settings", "ServiceWS_SrvWebReference_Service", "value")</example>
    public static string RetrieveApplicationSetting(string configSection, string subSection, string innersection, string propertyName) {
        string result = string.Empty;
        HttpWorkerRequest fakeWorkerRequest = null;
        try {
            using (TextWriter textWriter = new StringWriter()) {
                fakeWorkerRequest = new SimpleWorkerRequest("default.aspx", "", textWriter);
                var fakeHTTPContext = new HttpContext(fakeWorkerRequest);
                Configuration config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap() { ExeConfigFilename = fakeHTTPContext.Server.MapPath(@"~/Web.config") }, ConfigurationUserLevel.None);
                ConfigurationSectionGroup group = config.SectionGroups[configSection];
                if (group != null) {
                    ClientSettingsSection clientSection = group.Sections[subSection] as ClientSettingsSection;
                    if (clientSection != null) {
                        SettingElement settingElement = clientSection.Settings.Get(innersection);
                        if (settingElement != null) {
                            result = (((SettingValueElement)(settingElement.ElementInformation.Properties[propertyName].Value)).ValueXml).InnerText;
                        }
                    }
                }
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            fakeWorkerRequest.CloseConnection();
        }
        return result;
    }
}

https://www.ServiceWS.com/webservices/Golf

Depends something like this.

  var s = SDHSServer.Properties.Settings.Default.DOServer_WebReference1_Service;

or

  var s = SDHSServer.Properties.Settings.DOServer_WebReference1_Service;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top