【转】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 ...
随机推荐
- PAT 1068 万绿丛中一点红
https://pintia.cn/problem-sets/994805260223102976/problems/994805265579229184 对于计算机而言,颜色不过是像素点对应的一个 ...
- express和数据库(MySQL)的交互(二)
一.安装express前面都讲了 1.express. cnpm || npm install express --save 2.中间件 a.cnpm || npm install body-pars ...
- Android bp语法介绍
参考网址: http://note.qidong.name/demo/soong_build/ 谷歌官网文档: https://android.googlesource.com/platform/bu ...
- mysql索引利弊分析
转载自:http://blog.csdn.net/linminqin/article/details/44342205 索引的利弊与如何判定,是否需要索引 相信读者都知道索引能够极大地提高数据检索的 ...
- 微信小程序组件 模块化错和叹号
wxml 页面 <import src="/pages/lianxi/lianxi.wxml" /> //引入文件 <view style='position: ...
- HTML标签参考手册
按字母顺序排列 New : HTML5 中的新标签. 标签 描述 <!--...--> 定义注释. <!DOCTYPE> 定义文档类型. <a> 定义锚. < ...
- DAY2-Python学习笔记
1.迭代器:可以直接作用于for循环的对象统称为可迭代对象:Iterable,使用isinstance()判断一个对象是否是Iterable对象: >>> from collecti ...
- PGM学习之三 朴素贝叶斯分类器(Naive Bayes Classifier)
介绍朴素贝叶斯分类器的文章已经很多了.本文的目的是通过基本概念和微小实例的复述,巩固对于朴素贝叶斯分类器的理解. 一 朴素贝叶斯分类器基础回顾 朴素贝叶斯分类器基于贝叶斯定义,特别适用于输入数据维数较 ...
- java的4种引用 强软弱虚
&lt;img src="https://pic4.zhimg.com/d643d9ab5c933ac475cfa23063bed137_b.png" data-r ...
- 页面: Fork me on GitHub
一.实现效果如下: 二.代码地址:https://github.com/blog/273-github-ribbons 这是一个国外网友开发的代码, 里面有很多种样式,可以自已随心选择. 三.我们只拿 ...