一、获取当前文件的路径

1.  System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName  获取模块的完整路径,包括文件名。

2.  System.Environment.CurrentDirectory    获取和设置当前目录(该进程从中启动的目录)的完全限定目录。

3.  System.IO.Directory.GetCurrentDirectory()    获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:\www里,这个函数有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有时不一定返回什么东东,这是任何应用程序最后一次操作过的目录,比如你用Word打开了E:\doc\my.doc这个文件,此时执行这个方法就返回了E:\doc了。

4. System.AppDomain.CurrentDomain.BaseDirectory    获取程序的基目录。

5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase    获取和设置包括该应用程序的目录的名称。

6. System.Windows.Forms.Application.StartupPath    获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已

7. System.Windows.Forms.Application.ExecutablePath    获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。

二、操作环境变量    利用System.Environment.GetEnvironmentVariable()方法可以很方便地取得系统环境变量,如:    System.Environment.GetEnvironmentVariable("windir")就可以取得windows系统目录的路径。

以下是一些常用的环境变量取值:

System.Environment.GetEnvironmentVariable("windir");

System.Environment.GetEnvironmentVariable("INCLUDE");

System.Environment.GetEnvironmentVariable("TMP");

System.Environment.GetEnvironmentVariable("TEMP");

System.Environment.GetEnvironmentVariable("Path");

最后贴出我进行上面操作获得的变量值,事先说明,本人是编写了一个WinForm程序,项目文件存放于D:\Visual Studio Projects\MyApplication\LifeAssistant,编译后的文件位于D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug,最后的结果如下:

1、System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\LifeAssistant.exe

2、System.Environment.CurrentDirectory=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug

3、System.IO.Directory.GetCurrentDirectory()=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug

1 asp.net webform用"Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含"\";

2.c# winform用

A:"Application.StartupPath":获取当前应用程序所在目录的路径,最后不包含"\";

B:"Application.ExecutablePath ":获取当前应用程序文件的路径,包含文件的名称;

C:"AppDomain.CurrentDomain.BaseDirectory":获取当前应用程序所在目录的路径,最后包含"\";

D:"System.Threading.Thread.GetDomain().BaseDirectory":获取当前应用程序所在目录的路径,最后包含"\";

E:"Environment.CurrentDirectory":获取当前应用程序的路径,最后不包含"\";

F:"System.IO.Directory.GetCurrentDirectory":获取当前应用程序的路径,最后不包含"\";    3.c# windows service用"AppDomain.CurrentDomain.BaseDirectory"或"System.Threading.Thread.GetDomain().BaseDirectory";   用"Environment.CurrentDirectory"和"System.IO.Directory.GetCurrentDirectory"将得到" system32"目录的路径;

如果要使用"Application.StartupPath"或"Application.ExecutablePath ",需要手动添加对"System.Windows.Forms.dll "的引用,并在程序开头用"using System.Windows.Forms"声明该引用;

4.在卸载程序获取系统安装的目录:

System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();

string path=curPath.Location;//得到安装程序类SetupLibrary文件的路径,获取这个文件路径所在的目录即得到安装程序的目录;

4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\

5、System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\

6、System.Windows.Forms.Application.StartupPath=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug

7、System.Windows.Forms.Application.ExecutablePath=D:\Visual Studio Projects\MyApplication\LifeAssistant\bin\Debug\LifeAssistant.exe    System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS

System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\

System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp

System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\zhoufoxcn\LOCALS~1\Temp

System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\jdk1.5.0\bin;C:\MySQLServer5.0\bin;C:\Program Files\Symantec\pcAnywhere\;C:\Program Files\Microsoft SQL Server\80\Tools\BINN

C# 相对路径 系统路径

//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

string  str5=Application.StartupPath;    //可获得当前执行的exe的文件名。

string  str1  =Process.GetCurrentProcess().MainModule.FileName;    //获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。备注  按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如"C:\")。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如"C:\mySubDirectory")。

string  str2=Environment.CurrentDirectory;    //获取应用程序的当前工作目录。

string  str3=Directory.GetCurrentDirectory();    //获取基目录,它由程序集冲突解决程序用来探测程序集。  string  str4=AppDomain.CurrentDomain.BaseDirectory;    //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。  string  str5=Application.StartupPath;    //获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

string  str6=Application.ExecutablePath;    //获取或设置包含该应用程序的目录的名称。    string  str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;    //例子    Application.StartupPath;    //可以得到F:\learning\c#Training\win\win\bin\Debug    //注意自己补两个\    Application.StartupPath+"\\3.jpg";

在c#中,相对路径是用"."和".."表示,   "."代表当前目录,    ".."代表上一级录。    例如 假设我用vs2005在D:\My Documents\Visual Studio 2005\Projects目录里创建了一个名叫controls的项目,即在Projects文件夹里有一个controls文件夹,controls文件夹里有三个文件:controls.sln  controls文件夹  GulfOfStLawrence文件夹。    D:\My Documents\Visual Studio 2005\Projects\Controls\Controls\bin\Debug是这个简单项目能够运行的可执行文件Controls.exe    现在我想要 D:\My Documents\Visual Studio 2005\Projects\Controls\GulfOfStLawrence文件夹下的Gulf_of_St._Lawrence.mxd(arcgis desktop)工程文件路径。    那么相对路径应该就是"..\..\..\GulfOfStLawrence\Gulf_of_St._Lawrence.mxd"    即string filename = @"..\..\..\GulfOfStLawrence\Gulf_of_St._Lawrence.mxd";

心得:

1.用相对路径能增加项目的可移植性。使一个工程在移植过程中变得简单,节省了大量布置与工程相关的文件的时间。(如果设置的是绝对路径)。

2.使用相对路径也使程序代码变得简单

3. 但有一点必须注意:(只能在同一个驱动器里(如:都在D:里)使用相对路径)。

C#_获取路径的更多相关文章

  1. Java中获取路径的方法_自我分析

    就目前的我来说最常用的两种获取路径的方法是  class.getRecource(filename) 和 class.getclassloader.getRecource(filename) 这两者的 ...

  2. java中获取路径的几种基本的方法

    package com.ygh.blog.realpath; import java.io.File; import java.io.IOException; import java.io.Input ...

  3. JAVA中获取路径

    内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径:   以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...

  4. C#项目打开/保存文件夹/指定类型文件,获取路径

    C#项目打开/保存文件夹/指定类型文件,获取路径 转:http://q1q2q363.xiaoxiang.blog.163.com/blog/static/1106963682011722424325 ...

  5. find_first_of()和 find_last_of() 【获取路径、文件名】

    find_first_of()和 find_last_of() [获取路径.文件名](2011-06-11 12:44:46)转载▼标签: 杂谈 分类: c  string 类提供字符串处理函数,利用 ...

  6. Java文件获取路径方式:

    转自:http://blog.csdn.net/appleprince88/article/details/11599805# 谢谢! 由于经常需要获取文件的路径,但是比较容易忘记,每次需要总需要查询 ...

  7. Java获取路径方法&相对路径读取xml文件方法

    (1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...

  8. asp.net后台获取路径的各种方法归纳

    asp.net后台获取路径的各种方法归纳   1.Request.CurrentExecutionFilePath    获取当前请求的虚拟路径,不同于 FilePath,差别在于如果请求已在服务器代 ...

  9. ASP.NET获取路径的方法

    原文:[转载]ASP.NET获取路径的方法 HttpContext.Current.Request.PhysicalPath;    // 获得当前页面的完整物理路径.比如 F:\XFU.NSQS\p ...

随机推荐

  1. Linux 内存文件系统

    Linux内存文件系统:可满足高IO的要求 ramdisk: 基于虚拟在内存中的其他文件系统(ex2fs). 挂载方式:mount /dev/ram /mnt/ramdisk ramfs: 物理内存文 ...

  2. 【PAT】B1040 有几个PAT(25)(25 分)

    一点25分的样子都没有 #include<cstdio> #include<string.h> using namespace std; int main(){ long lo ...

  3. 学生与部门管理app-产品功能与界面的简单设计

    学生与部门管理app-产品功能与界面的简单设计 1. 结对成员学号 我:********* 大佬:*******10 2. 需求分析(NABCD模型) 2.1 N-需求 各个部门在开学初占据学校青春广 ...

  4. SpringMVC 使用JSR-303进行校验 @Valid

    注意:1 public String save(@ModelAttribute("house") @Valid House entity, BindingResult result ...

  5. 【记录】GIT 常用命令记录

    1. 查看所有的提交版本,包含当你co到之前提交版本后依旧可以看到以前的日志 git log --graph --pretty=format:'%h -%d %s (%cr)' --abbrev-co ...

  6. 【原创】Linux常用命令记录

    1. 查看网络状态分布 #!/bin/sh netstat -apn >/dev/ \ | awk 'BEGIN {printf("%-15s%-15s%-15s%-15s\n&quo ...

  7. 什么是Java序列化?如何实现序列化?

    一.什么是序列化: 序列化理解成“打碎”是可以的,不过在书本上的名词就是将对象转换成二进制. 二.在java中如何实现序列化: 首先我们要把准备要序列化类,实现 Serializabel接口 例如:我 ...

  8. c++11の多线程应用

    std::thread 应用十分方便,通过#include<thread>引入 std::thread t(subFunction); t.join(); 主线程将等待子线程完成后,继续调 ...

  9. (3)HomeAssistant 连接MQTT

    整体说明 1 自己在阿里云上搭建MQTT服务器 2 自己笔记本电脑windos10搭建HASS,配置参数连接阿里云服务器 3 手机下载MQTT调试助手,当测试端 4手机当终端---阿里云MQTT--- ...

  10. maven的build

    1.maven-compiler-plugin 作用 编译作用java中获取接口(方法)中的参数名字(eclipse设置编译参数)(java8 javac -parameters)https://bl ...