十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
控制臺應(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();
//替換掉雙引號
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ù)、控制臺等應(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;
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。