【转】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 ...
随机推荐
- 很有用的高级 Git 命令
10 个很有用的高级 Git 命令 迄今,我已经使用Git很长一段时间了,考虑分享一些不管你是团队开发还是个人项目,都受用的高级git命令. 1. 输出最后一次提交的改变 这个命令,我经常使用它 来发 ...
- CentOS7 修改 启动级别
1. centos7 之前应该使用init 的启动脚本 不支持并行 速度比较慢, centos7 开始使用systemd 的模式 提高了开机的性能 所以之前的init 脚本修改 启动级别应该就无效了 ...
- maven下载、安装、卸载以及MyEclipse配置maven
maven下载 官网下载:http://maven.apache.org/download.cgi 点击链接为官网下载页面,翻到下图所示位置,点击红框选项即可下载 maven安装 1.解压 ...
- delphi如何检索adoquery里面某一列存在的重复行?
var IsHave:Boolean; begin adoquery.first; while(not adoquery.eof) do begin if(adoquery.fieldbyname(' ...
- IE Only的userData
上次我们提到了本地存储的一个方式,那就是Cookie,不过遗憾的是Cookie保存的数据量非常小,更详细的可以参考<在 Internet Explorer 中的 cookie 的数字和大小限制& ...
- Springboot 和 Mybatis集成开发
Springboot 和 Mybatis集成开发 本项目使用的环境: 开发工具:Intellij IDEA 2017.1.3 jdk:1.7.0_79 maven:3.3.9 额外功能 PageHel ...
- linux系统下 git 使用教程
一.初始化 1.首先安装git软件,安装环境是centos 7.x下的云服务器.使用命令: #yum install git 2.设置用户名和邮箱(必须): # git config --global ...
- 【刷题】BZOJ 4530 [Bjoi2014]大融合
Description 小强要在N个孤立的星球上建立起一套通信系统.这套通信系统就是连接N个点的一个树. 这个树的边是一条一条添加上去的.在某个时刻,一条边的负载就是它所在的当前能够 联通的树上路过它 ...
- [AT697]フィボナッチ
题目大意:给你$n,k(n\leqslant10^9,k\leqslant10^3)$,求$f_n$.$f$数组满足$f_1=f_2=\cdots=f_k=1$,$f_n=\sum\limits_{i ...
- vi写完文件保存时才发现是readonly😂
在MAC上编辑apache配置文件,老是忘记sudo…… readonly的文件保存时提示 add ! to override, 但这仅是对root来说的啊! 百毒了一下竟然还有解决方案!! :w ! ...