創(chuàng)新互聯(lián)基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供成都移動(dòng)云計(jì)算中心 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。
控制臺(tái)應(yīng)用程序:Environment.CurrentDirectory、Directory.GetCurrentDirectory()
windows服務(wù):Environment.CurrentDirectory
windows服務(wù)安裝成功后:
/// /// public static string GetWindowsServiceInstallPath(string ServiceName)
{
string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;
string path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();
//替換掉雙引號(hào)
path = path.Replace("\"", string.Empty);
FileInfo fi = new FileInfo(path);
return fi.FullName;
//return fi.FullName.Directory.ToString();
}
//windows 服務(wù)中使用log4net
string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath);
string configFilePath = assemblyDirPath + "http://log4net.config";
DOMConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
///
/// 獲取應(yīng)用程序web.config中的文件配置路徑,并返回物理路徑
/// 適用于web應(yīng)用程序
///
///
///
public static string GetFileFullpath(string key)
{
if (string.IsNullOrEmpty(key)) return string.Empty;
//獲取應(yīng)用程序的web.config中配置的路徑
string appSetting = System.Configuration.ConfigurationManager.AppSettings[key].ToString();
//如果到的路徑不是物理路徑,則映射為物理路徑
if (!Path.IsPathRooted(appSetting)) appSetting = System.Web.HttpContext.Current.Server.MapPath(appSetting);
return appSetting;
}
///
/// 獲取應(yīng)用程序.config中的文件配置路徑,并返回物理路徑
/// 適用于windows服務(wù)、控制臺(tái)等應(yīng)用程序
///
///
///
public static string GetAssemblyPath(string key)
{
if (string.IsNullOrEmpty(key)) return string.Empty;
//獲取應(yīng)用程序的web.config中配置的路徑
string appSetting = System.Configuration.ConfigurationManager.AppSettings[key].ToString();
//如果到的路徑不是物理路徑,則映射為物理路徑
if (!Path.IsPathRooted(appSetting))
{
string assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string dirName = Path.GetDirectoryName(assemblyPath);
if (dirName.IndexOf(@"\bin\Debug") > -1)
appSetting = dirName.Replace(@"\bin\Debug", appSetting.Substring(1).Replace(@"/", @"\"));
else
appSetting = dirName + appSetting.Substring(1).Replace(@"/", @"\");
}
return appSetting;
}
///
/// 獲取應(yīng)用程序.config中的文件配置路徑,并返回物理路徑
/// 適用于windows服務(wù)應(yīng)用程序的成功安裝之后
///
///
///
public static string GetInstallPath(string key)
{
if (string.IsNullOrEmpty(key)) return string.Empty;
//獲取應(yīng)用程序的web.config中配置的路徑
string appSetting = System.Configuration.ConfigurationManager.AppSettings[key].ToString();
//如果到的路徑不是物理路徑,則映射為物理路徑
if (!Path.IsPathRooted(appSetting))
{
string processPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
appSetting = processPath.Substring(0, processPath.LastIndexOf(@"\")) + appSetting.Substring(1).Replace(@"/", @"\");
}
return appSetting;
}