技术开发 频道

C#创建Windows服务


【IT168技术文档】

  在Windows服务里面,如果访问文件,采用绝对路径可以.如果采用相对路径,和生成的服务文件在同一目录下面.则要访问此程序集 下面的文件.
using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.Xml; using System.IO; namespace HDUSearch ...{ public class IndexConfig ...{ GetValue#region GetValue public string GetValue(string key) ...{ string assemblyFilePath = Assembly.GetExecutingAssembly().Location; string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(assemblyDirPath + "\Index.config"); XmlNodeList nodeList = xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes; //获取appSettings节点的所有子节点 foreach (XmlNode xn in nodeList) //遍历所有子节点 ...{ XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 if (xe.GetAttribute("key").IndexOf(key) != -1) ...{ return xe.GetAttribute("value"); break; } } return ""; } #endregion } }
0
相关文章