控制台应用程序: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各种应用程序中获取文件路径的方法的更多相关文章

  1. Android中获取文件路径的方法总结及对照

    最近在写文件存贮,Android中获取文件路径的方法比较多,所以自己也很混乱.找了好几篇博客,发现了以下的路径归纳,记录一下,以备不时之需 Environment.getDataDirectory() ...

  2. IOS中获取文件路径的方法

    iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...

  3. Java中获取文件路径

    Java中获取文件路径 1.实例说明 (1)得到 ClassPath的绝对URI路径 Thread.currentThread().getContextClassLoader().getResourc ...

  4. web项目中获取各种路径的方法

    ~Apple   web项目中各种路径的获取 1.可以在servlet的init方法里 String path = getServletContext().getRealPath("/&qu ...

  5. SWIFT中获取配置文件路径的方法

    在项目中有时候要添加一些配置文件然后在程序中读取相应的配置信息,以下为本人整理的获取项目配置文件(.plist)路径的方法: 1.获取沙盒路径后再APPEND配置文件 func documentsDi ...

  6. Delphi的TService 服务路径获取 Dll中获取文件路径

    研究delphi服务的路径,试了好几个方法 ,都没取出来,最后发现,要采用取DLL路径的方法 //一.获取Dll自身路径 //1)方法一: Function GetDllPath(sDllName:s ...

  7. java项目中获取文件路径的几种方法

    // 第一种: 2 File f = new File(this.getClass().getResource("/").getPath()); // 结果: /Users/adm ...

  8. java中获取文件路径的几种方式

    http://xyzroundo.iteye.com/blog/1116159关于绝对路径和相对路径: 绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:xyz es ...

  9. python中获取文件路径的几种方式

    # 如果执行文件为E:\aa\bb\aa.py 1.获取当前路径 current_path11 = os.path.abspath(__file__) current_path12 = os.path ...

随机推荐

  1. paperOne基于java web的简易四则运算出题网站

    项目成员:张金生     张政 需求概要 1.运算数均为正整数 2.包含的运算符有+,-,*,/ 3.除法运算结果为整除运算 4.批量生成题目并判题 核心功能分析 1.题目生成——java后端 题目生 ...

  2. Linux上两种网络连接方式

    模式一:NAT方式好处:路由器更换,或者交换机更换,网络仍然可以使用,所用使用最多 准备工作: 查看VMware服务器启动情况,五个全开模式 vmnet8开启模式 1 配置VMware交换机的ip地址 ...

  3. day02--Python基础二(基础数据类型)

    一.数据与数据类型 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 数字(int) 字符串 ...

  4. H5实现的时钟

    源码如下: <!doctype html> <html> <head></head> <body> <canvas id=" ...

  5. 【转载】JSP生成静态Html页面

    在网站项目中,为了访问速度加快,为了方便百度爬虫抓取网页的内容,需要把jsp的动态页面转为html静态页面.通常有2种常用的方式: 1.伪静态,使用URL Rewriter 2.纯静态,本文中代码实现 ...

  6. Redis 基础:Redis 配置

    Redis 配置 Redis的配置文件位于Redis安装目录下,文件名为redis.conf.可以通过CONFIG命令查看或设置配置项.其语法为: # Redis CONFIG命令格式如下: > ...

  7. Spring(1):Spring简介

    一句话概括: Spring是一种轻量级控制反转IoC和面向切面AOP的容器框架 初衷: 使用接口编程而不是类 为javabean提供一个更好的应用配置框架 javabean是一种规范而不是技术.是指符 ...

  8. 理解Restful api的意义

    RESTful API 只是API的设计规范或者是一套设计理论. 单就URL和Method这两个点,你可以这样理解: URL 是用来唯一标示一个互联网资源的,而 Method 是用来标识当前请求对该资 ...

  9. hdu 5638 Toposort (拓扑排序+线段树)

    Toposort Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  10. [AT1999] [agc002_e] Candy Piles

    题目链接 AtCoder:https://agc002.contest.atcoder.jp/tasks/agc002_e 洛谷:https://www.luogu.org/problemnew/sh ...