Reputation: 384
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 :
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;
}
//strDir = ConfigurationManager.AppSettings["Directory"];
//fileMask = ConfigurationManager.AppSettings["FileMask"];
//strBatfile = ConfigurationManager.AppSettings["Batch"];
//strlog = ConfigurationManager.AppSettings["Log"];
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()));
}
}
but something like.
//strDir = ConfigurationManager.AppSettings["Directory"];
//fileMask = ConfigurationManager.AppSettings["FileMask"];
//strBatfile = ConfigurationManager.AppSettings["Batch"];
//strlog = ConfigurationManager.AppSettings["Log"];
is not working..Also the xml is not available while release mode.
Can someone give me way to access app config to pass input param to Window service.
Upvotes: 1
Views: 3653
Reputation: 17485
If your window service application produces a WindowService1.exe then your configuration file App.config will get changed to WindowService1.exe.config.
Now to read from the config file you can use
ConfigurationManager.AppSettings["key"]
If you are not able to get ConfigurationManager then you have to add a reference to the System.Configuration assembly.
Upvotes: 1