【转】c#.net各种应用程序中获取文件路径的方法
控制台应用程序:Environment.CurrentDirectory、Directory.GetCurrentDirectory()
windows服务:Environment.CurrentDirectory
windows服务安装成功后:
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
2. ///
/// 获取服务应用程序的安装路径(或者当前安装目录)///
/// /// 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 服务中使用log4net
string assemblyFilePath = Assembly.GetExecutingAssembly().Location;
string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath);
string configFilePath = assemblyDirPath + "//log4net.config";
DOMConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));
/// <summary>
/// 获取应用程序web.config中的文件配置路径,并返回物理路径
/// 适用于web应用程序
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetFileFullpath(string key)
{
if (string.IsNullOrEmpty(key)) return string.Empty;
//获取应用程序的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>
/// 获取应用程序.config中的文件配置路径,并返回物理路径
/// 适用于windows服务、控制台等应用程序
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetAssemblyPath(string key)
{
if (string.IsNullOrEmpty(key)) return string.Empty;
//获取应用程序的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>
/// 获取应用程序.config中的文件配置路径,并返回物理路径
/// 适用于windows服务应用程序的成功安装之后
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetInstallPath(string key)
{
if (string.IsNullOrEmpty(key)) return string.Empty;
//获取应用程序的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;
}
转载:http://blog.csdn.net/cafuc229/article/details/7667172
【转】c#.net各种应用程序中获取文件路径的方法的更多相关文章
- Android中获取文件路径的方法总结及对照
最近在写文件存贮,Android中获取文件路径的方法比较多,所以自己也很混乱.找了好几篇博客,发现了以下的路径归纳,记录一下,以备不时之需 Environment.getDataDirectory() ...
- IOS中获取文件路径的方法
iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...
- Java中获取文件路径
Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...
- web项目中获取各种路径的方法
~Apple web项目中各种路径的获取 1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/&qu ...
- SWIFT中获取配置文件路径的方法
在项目中有时候要添加一些配置文件然后在程序中读取相应的配置信息,以下为本人整理的获取项目配置文件(.plist)路径的方法: 1.获取沙盒路径后再APPEND配置文件 func documentsDi ...
- Delphi的TService 服务路径获取 Dll中获取文件路径
研究delphi服务的路径,试了好几个方法 ,都没取出来,最后发现,要采用取DLL路径的方法 //一.获取Dll自身路径 //1)方法一: Function GetDllPath(sDllName:s ...
- java项目中获取文件路径的几种方法
// 第一种: 2 File f = new File(this.getClass().getResource("/").getPath()); // 结果: /Users/adm ...
- java中获取文件路径的几种方式
http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz es ...
- python中获取文件路径的几种方式
# 如果执行文件为E:\aa\bb\aa.py 1.获取当前路径 current_path11 = os.path.abspath(__file__) current_path12 = os.path ...
随机推荐
- ansible的简单使用
环境搭建跳过(暂无,这个以后再学习学习,不要在意这些细节) 首先,在环境搭建好后,用两台虚机来做测试,一台192.168.181.130做测试机,一台192.168.181.131为批量处理服务器 编 ...
- Beta阶段冲刺前的准备
Beta阶段冲刺前的准备 凡事预则立,在Beta开始前,以小组为单位,在敏捷冲刺前发布一篇博客,描述: 1. 讨论组长是否重选的议题和结论 经过我们小组在周二下午的会议中有重新认真的考虑了是否要更换组 ...
- Java 使用 DBCP mysql 连接池 做数据库操作
需要的jar包有 commons-dbutils , commons-dbcp , commons-pool , mysql-connector-java 本地database.propertties ...
- 队列java实现
队列是一种线性数据结构,是一种运算受限的线性表,只允许在队尾插入,在队头删除.运算规则是先进先出.恰好和栈相反.栈是先进后出.因为栈只在栈顶做删除和插入. 队列按照存储结构可以分为顺序队列和链式队列. ...
- TeX-换行换页与段落命令
换行换页与段落命令1 UTF8nsung Abstract 文档在排版时往往要求每一行具有相同的长度, LATEX 为了对整段的文挡进行优化,将插入必要的换行和空恪.如果必要的话对于一行中不好放的单词 ...
- CF702F T-Shirts
题目描述 The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In ...
- BZOJ 2337 XOR和路径 | 高斯消元 期望 位运算
BZOJ 2337 XOR和路径 题解 这道题和游走那道题很像,但又不是完全相同. 因为异或,所以我们考虑拆位,分别考虑每一位: 设x[u]是从点u出发.到达点n时这一位异或和是1的概率. 对于所有这 ...
- 【IOI 2018】Doll 机械娃娃
我感觉这个题作为Day2T1,有一定的挑战性.为$Rxd$没有完成这道题可惜. 我觉得这道题,如果按照前几个部分分的思路来想,就有可能绕进错误的思路中.因为比如说每个传感器最多只在序列中出现$2$次, ...
- Linux内核分析实验四----
一.用户态.内核态 权限分级——为了系统本身更稳定,使系统不宜崩溃.(并不是所有程序员缩写的代码都很健壮!!) x86 CPU四种不同的执行级别:0(内核态)—3(用户态) 区分方法:CS:EIP(C ...
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...