从App.config访问应用程序设置 [英] Access app setting from App.config

查看:98
本文介绍了从App.config访问应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我写了一个窗口服务,它从xml文件中获取输入。该文件放在.exe本身的文件夹中。但是我希望该窗口服务从App.config获取输入。

我读到App配置名称变为service.exe.config并且每个地方都有了解决方案我们如何从那里访问连接字符串。可以有人告诉我一种方法从窗口服务中的应用程序配置中获取应用程序设置。



我有一个谷歌搜索很多,但可以这样做。

当前代码为:



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 :

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.

推荐答案

啊...使用相同的代码。刚注意到在window service.Config文件名被更改。

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



配置名称改为.exe.config


Config name ets changed to .exe.config


这并不像你理解那么困难。在窗口服务中,将EXE和App.config放在同一个文件夹中,然后使用

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
ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;


这篇关于从App.config访问应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆