C#_获取路径
一、获取当前文件的路径
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#_获取路径的更多相关文章
- Java中获取路径的方法_自我分析
就目前的我来说最常用的两种获取路径的方法是 class.getRecource(filename) 和 class.getclassloader.getRecource(filename) 这两者的 ...
- java中获取路径的几种基本的方法
package com.ygh.blog.realpath; import java.io.File; import java.io.IOException; import java.io.Input ...
- JAVA中获取路径
内容来自于snannan_268 关键字: java中获取路径 JAVA中获取路径: 1.jsp中取得路径: 以工程名为TEST为例: (1)得到包含工程名的当前页面全路径:request.get ...
- C#项目打开/保存文件夹/指定类型文件,获取路径
C#项目打开/保存文件夹/指定类型文件,获取路径 转:http://q1q2q363.xiaoxiang.blog.163.com/blog/static/1106963682011722424325 ...
- find_first_of()和 find_last_of() 【获取路径、文件名】
find_first_of()和 find_last_of() [获取路径.文件名](2011-06-11 12:44:46)转载▼标签: 杂谈 分类: c string 类提供字符串处理函数,利用 ...
- Java文件获取路径方式:
转自:http://blog.csdn.net/appleprince88/article/details/11599805# 谢谢! 由于经常需要获取文件的路径,但是比较容易忘记,每次需要总需要查询 ...
- Java获取路径方法&相对路径读取xml文件方法
(1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...
- asp.net后台获取路径的各种方法归纳
asp.net后台获取路径的各种方法归纳 1.Request.CurrentExecutionFilePath 获取当前请求的虚拟路径,不同于 FilePath,差别在于如果请求已在服务器代 ...
- ASP.NET获取路径的方法
原文:[转载]ASP.NET获取路径的方法 HttpContext.Current.Request.PhysicalPath; // 获得当前页面的完整物理路径.比如 F:\XFU.NSQS\p ...
随机推荐
- [20171101]修改oracle口令安全问题.txt
[20171101]修改oracle口令安全问题.txt --//等保的问题,做一些关于修改oracle口令方面的测试. 1.oracle修改口令一般如下方式: alter user scott id ...
- php中编码转换方法
php里经常用到编码转换,在这记录一个常用的编码转换方法,字符串.数组.对象都可以使用,使用了递归来解决,比较普通 /* * php中编码转换 * @param $param 需要转换的数据 * @p ...
- 【PAT】B1082 射击比赛(20 分)
水提水题,直接贴代码啦 #include<cstdio> #include<algorithm> using namespace std; struct ppp{ int id ...
- arcgis如何求两个栅格数据集的差集
栅格数据集没有擦除功能,现在有栅格A和栅格B,怎么求两个栅格的差集C 具体步骤如下: 1.首先利用栅格计算器,把栅格B中的value全部赋值为0 输入语句:"栅格B" * 0 2 ...
- ES5-ES6-ES7_let关键字声明变量
let命令的介绍 let是ECMAScript6中新增的关键字,用于声明变量.它的用法类似于var var a = 3 let b = 4 let变量的声明 let 命令的特点不允许在同一作用域下声明 ...
- C/C++每日小练(七)——墓地雕塑
墓地雕塑 题目描写叙述: 在一个周长为10000的圆上等距分布着n个雕塑. 如今又有m个新雕塑增加(位置能够任意放).希望全部n+m个雕塑在圆周上均匀分布.这就须要移动当中一些原有的雕塑.要求n个雕塑 ...
- 转://使用showplan.sql分析sql Performance
在HelloDBA网站找到一个分析sql性能的工具—showplan,记录一下 showplan.sql下载路径:http://www.HelloDBA.com/Download/showplan.z ...
- 转://对于11gR2的集群relink
对于11gR2的集群relink参考MOS:Do I need to relink the Oracle Clusterware / Grid Infrastructure home after an ...
- mocha测试框架-truffle
https://mochajs.org/ 学习网址: https://www.jianshu.com/p/9c78548caffa https://www.jb51.net/article/10646 ...
- solidity-library
library 1)直接使用使用库合约的合约,可以将库合约视为隐式的父合约(base contracts),当然它们不会显式的出现在继承关系中.意思就是不用写is来继承,直接可以在合约中使用: lib ...