//取服务安装路径
public System.String Get_WindowService_Path()
{
System.String str_Path = System.String.Empty;
try
{
//取动态库地址,即.exe文件运行的地址、或Window服务运行启动文件的地址
System.String strAssemblyFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
//截取除了动态库运行文件名的地址
System.String AppPath = System.IO.Path.GetDirectoryName(strAssemblyFilePath);
str_Path = AppPath;
}
catch (Exception ex)
{
System.String path_service = this.Get_WindowService_Path();
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path_service + “\异常.txt”, false))
{
sw.WriteLine(“于” + DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss”) + ex.ToString());
}
}
return str_Path;
}
使用场景: