Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have written a window service and it is taking input from an xml file.That file is placed in the folder of .exe itself.But i want that window service to take input from App.config.
I read that App config name gets changed to service.exe.config and every whereit is mentined how can we acces connection string from that.Can someone tell me a way to get app setting from app config in window service.

I have a googled a lot but could do that.
Current code is :

C#
protected override void OnStart(string[] args)
{

     current_directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    XmlDocument xml = new XmlDocument();
    try
    {
          string Xml_Path = System.AppDomain.CurrentDomain.BaseDirectory;
         xml.Load(current_directory+"\\Data.xml");//suppose that myXmlString contains "<Names>...</Names>"


            XmlNodeList xnList = xml.SelectNodes("/Names/Name");
             foreach (XmlNode xn in xnList)
             {
                 strDir = xn["Directory"].InnerText;
                 fileMask = xn["FileMask"].InnerText;
                 strBatfile = xn["Batch"].InnerText;
                 strlog = xn["Log"].InnerText;


             }

        m_Watcher = new FileSystemWatcher();


        m_Watcher.Filter = fileMask;
        m_Watcher.Path = strDir + "\\";
        m_Watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                         | NotifyFilters.FileName | NotifyFilters.DirectoryName;




        m_Watcher.Created += new FileSystemEventHandler(OnCreated);

        m_Watcher.Deleted += new FileSystemEventHandler(OnDeleated);
        m_Watcher.Renamed += new RenamedEventHandler(OnRenamed);


        m_Watcher.EnableRaisingEvents = true;
    }
    catch (Exception exception)
    {
        CustomException.Write(CustomException.CreateExceptionString(exception.ToString()));
    }

}


Thanks in Advance.
Posted

ah...used the same code.Just noticed that in window service.Config file name gets changed.
C#
strDir = ConfigurationManager.AppSettings["Directory"];
                fileMask = ConfigurationManager.AppSettings["FileMask"];
                strBatfile = ConfigurationManager.AppSettings["Batch"];
                strlog = ConfigurationManager.AppSettings["Log"];

Config name ets changed to .exe.config
 
Share this answer
 
It's not that difficult as you are understanding. In window service, put your EXE and App.config in the same folder and then you can access connection string using
C#
ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
 
Share this answer
 
v2
Comments
Praveen Kumar Upadhyay 18-Dec-14 6:41am    
Just wanted to know, who is this who has started devoting all my solutions and questions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900