获取window服务路径

来源:互联网 发布:c语言流程图生成器 编辑:程序博客网 时间:2024/03/29 07:57
        static public void WriteLog(string strLog)        {            //string sFilePath = "d:\\hsx\\" + DateTime.Now.ToString("yyyyMM");            string sFilePath = FilePath("RestartServiceServer") + "\\日志" + DateTime.Now.ToString("yyyyMM");            string sFileName = "errLog" + DateTime.Now.ToString("dd") + ".log";            sFileName = sFilePath + "\\" + sFileName; //文件的绝对路径            if (!Directory.Exists(sFilePath))//验证路径是否存在            {                Directory.CreateDirectory(sFilePath);//不存在则创建            }            FileStream fs;            StreamWriter sw;            if (File.Exists(sFileName))//验证文件是否存在,有则追加,无则创建            {                fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);            }            else            {                fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);            }            sw = new StreamWriter(fs);            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "     ---     " + strLog);            sw.Close();            fs.Close();        }        static public string FilePath(string serviceName)        {            RegistryKey _Key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\Services\" + serviceName);            if (_Key != null)            {                object _ObjPath = _Key.GetValue("ImagePath");                if (_ObjPath != null)                {                    string strPath = _ObjPath.ToString();                    return strPath.Substring(0, strPath.LastIndexOf('\\'));                }            }            return "";        }

0 0
原创粉丝点击