【发布时间】:2010-11-10 19:31:49
【问题描述】:
我正在用 C# 编写一个 Windows 服务,它会生成我正在编写的另一个应用程序的多个实例。该应用程序有可能安装在机器上的任何位置。让服务知道应用程序所在位置的最佳方式是什么?
【问题讨论】:
标签: c# .net windows windows-services installation
我正在用 C# 编写一个 Windows 服务,它会生成我正在编写的另一个应用程序的多个实例。该应用程序有可能安装在机器上的任何位置。让服务知道应用程序所在位置的最佳方式是什么?
【问题讨论】:
标签: c# .net windows windows-services installation
如果您需要找到安装服务的文件夹,可以使用以下代码
this.GetType().Assembly.Location
如果您需要找到安装了其他应用程序的文件夹,您应该向 Windows 安装程序提出请求
[DllImport("MSI.DLL", CharSet = CharSet.Auto)]
private static extern UInt32 MsiGetComponentPath(
string szProduct,
string szComponent,
StringBuilder lpPathBuf,
ref int pcchBuf);
private static string GetComponentPath(string product, string component)
{
int pathLength = 1024;
StringBuilder path = new StringBuilder(pathLength);
MsiGetComponentPath(product, component, path, ref pathLength);
return path.ToString();
}
【讨论】:
如果您的意思是服务启动了一个不同的应用程序,那么;选项:
就个人而言,我喜欢配置文件选项;它简单且易于维护,并允许多个单独(并排)的服务和应用程序安装
【讨论】:
using System.IO;
using System.Windows.Forms;
string appPath = Path.GetDirectoryName(Application.ExecutablePath)
这是一个应用程序(上)。
对于一个 asp.net 项目:
using System.Web;
HttpContext.Current.Server.MapPath( "place arguments here" );
【讨论】:
System.Environment.CurrentDirectory
【讨论】:
在安装过程中写入注册表变量,这样在交付升级时您可以读回之前写入的值并默认为用户之前选择的文件夹。
【讨论】: