控制台应用程序: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. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  2. 交换机、linux光衰查询

    RX收光,TX发光 一.交换机 命令: display interface transceiver brief 结果: ...... HW6851 10GE1/0/15 transceiver dia ...

  3. 每日站立会议——敏捷流程scrum实践

    每日站立会议是敏捷流程scrum中的很重要的一个制度之一. 功能:     1.快速同步进展,让项目组内部的员工互相了解彼此的进展,从而了解本项目的整体进展.      2.给每个人一种精神压力,信守 ...

  4. java如何写入txt文件

    public class TxtWrite { public static void main(String args[]){ contentToTxt("D:\\xyky.txt" ...

  5. ACM数论之旅16---母函数(又名生成函数)(痛并快乐着(╭ ̄3 ̄)╭)

    (前排出售零食瓜子) 前言: 母函数是个很难的东西,难在数学 而ACM中所用的母函数只是母函数的基础 应该说除了不好理解外,其他都是非常简单的 母函数即生成函数,是组合数学中尤其是计数方面的一个重要理 ...

  6. Java并发编程之线程生命周期、守护线程、优先级、关闭和join、sleep、yield、interrupt

    Java并发编程中,其中一个难点是对线程生命周期的理解,和多种线程控制方法.线程沟通方法的灵活运用.这些方法和概念之间彼此联系紧密,共同构成了Java并发编程基石之一. Java线程的生命周期 Jav ...

  7. Java词频统计

    public class WordCount { public static void main(String[] args) { String[] stopWords = { "" ...

  8. 图片上传预览,兼容IE

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 第220天:Angular---路由

    内容介绍,为什么要使用前端路由? 在2005左右,兴起了一种叫做ajax的技术,有了ajax之后,我们向服务端提交数据的时候就不再需要使用from表单去提交了,因为from表单之间的提交会导致页面之间 ...

  10. 【BZOJ2989】数列(二进制分组,主席树)

    [BZOJ2989]数列(二进制分组,主席树) 题面 BZOJ 权限题啊... Description 给定一个长度为n的正整数数列a[i]. 定义2个位置的graze值为两者位置差与数值差的和,即g ...