您好,登錄后才能下訂單哦!
控制臺(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 + "//log4net.config";
DOMConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
/// <summary>
/// 獲取應(yīng)用程序web.config中的文件配置路徑,并返回物理路徑
/// 適用于web應(yīng)用程序
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 獲取應(yīng)用程序.config中的文件配置路徑,并返回物理路徑
/// 適用于windows服務(wù)、控制臺(tái)等應(yīng)用程序
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 獲取應(yīng)用程序.config中的文件配置路徑,并返回物理路徑
/// 適用于windows服務(wù)應(yīng)用程序的成功安裝之后
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
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;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。