C#当前应用程序路径及环境变量
一、获取当前文件的路径
1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
获取模块的完整路径。可获得当前执行的exe的文件名。
2. System.Environment.CurrentDirectory 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。(备注:按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:\”)。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径[如“C:\mySubDirectory”])。
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.Windows.Forms.Application.StartupPath 获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。效果和2一样。只是5返回的字符串后面多了一个"\"而已。 6. System.Windows.Forms.Application.ExecutablePath 获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。 7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase 获取和设置包括该应用程序的目录的名称。
8.this.GetType().Assembly.Location
获取当前进程的完整路径,包含文件名。
二、操作环境变量
利用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:\Projects\Demo,编译后的文件位于D:\Projects\Demo\bin\Debug,最后的结果如下:
1、System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Projects\Demo\bin\Debug\Demo.vshost.exe 2、System.Environment.CurrentDirectory=D:\Projects\Demo\bin\Debug 3、System.IO.Directory.GetCurrentDirectory()=D:\Projects\Demo\bin\Debug 4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Projects\Demo\bin\Debug\ 5、System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Projects\Demo\bin\Debug\ 6、System.Windows.Forms.Application.StartupPath=D:\Projects\Demo\bin\Debug 7、System.Windows.Forms.Application.ExecutablePath=D:\Projects\Demo\bin\Debug\Demo.EXE
System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio.NET 2005\SDK\v2.0\include\ System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\
四、在dll中有时需要使用主调用程序中的资源,这就要正确获取调用程序的文件名及其路径等信息。这需要和调用dll本身的文件名和路径区分开来!
这就牵扯到System.Reflection.Assembly程序集类使用了。
GetExecutingAssembly : 获取包含当前执行的代码的程序集
GetCallingAssembly : 返回调用当前正在执行的方法的方法的 System.Reflection.Assembly
来自:http://blog.csdn.net/jzxcheng/article/details/31361263
C#当前应用程序路径及环境变量的更多相关文章
- Windows 系统下设置Nodejs NPM全局路径和环境变量配置
在nodejs的安装目录中找到node_modules\npm\.npmrc文件 修改如下即可: prefix = D:\tool\nodejs\node_globalcache = D:\tool\ ...
- NX二次开发-UFUN获取环境变量路径,将环境变量转换为字符串,字符串拼接UF_translate_variable
NX9+VS2012 #include <uf.h> UF_initialize(); //UFUN获取环境变量路径 //将环境变量转换为字符串 char* GetName = NULL; ...
- C#.net 获取当前应用程序所在路径及环境变量
一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...
- 【C#学习笔记】获取当前应用程序所在路径及环境变量
转自:http://www.cnblogs.com/netlyf/archive/2011/06/22/2086718.html 一.获取当前文件的路径 string str1=Process.Get ...
- C#获取当前应用程序所在路径及环境变量
一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...
- (转载)C#获取当前应用程序所在路径及环境变量
一.获取当前文件的路径 string str1=Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名. string st ...
- C#读取“我的文档”等特殊系统路径及环境变量
返回“我的文档”路径字符串 Environment.GetFolderPath(Environment.SpecialFolder.Personal) 本技巧使用GetFolderPath方法来获取指 ...
- jdk的下载路径和环境变量的配置
一:jdk百度网盘的下载路径: 链接:https://pan.baidu.com/s/1pF41oGcTqouULsWKEBn3hw 提取码:p1y2 复制这段内容后打开百度网盘手机App,操作更方便 ...
- Mac 将mysql路径加入环境变量
1.打开终端,输入: cd ~ 会进入~文件夹 2.然后输入:touch .bash_profile 回车执行后, 2.再输入:open -e .bash_profile 会在TextEdit中打开这 ...
随机推荐
- Tkinter教程之Checkbutton篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811306 #Tkinter教程之Checkbutton篇#Checkbutton又称为多选按 ...
- 一个考察for循环题 讨论一下
一道Java程序题,主要是考察for循环如下所示: public class Test { static boolean fun(char c) { System.out.print(c); retu ...
- 【SPOJ】Transposing is even more fun!
题意: 给出a.b 表示按先行后列的方式储存矩阵 现在要将其转置 可以交换两个点的位置 求最小操作次数 题解: 储存可以将其视为拉成一条链 设a=5.b=2 则在链上坐标用2^***(a,b)表示为( ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- HDU 2874 Connections between cities (LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题意是给你n个点,m条边(无向),q个询问.接下来m行,每行两个点一个边权,而且这个图不能有环路 ...
- iOS 通知中心 NSNotificationCenter
iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...
- MFC 视图、文档、框架(通讯)
CMainFrame * pMainWnd=(CMainFrame*)AfxGetApp()->m_pMainWnd;//主框架 CChildFrame * pChild = (CChildFr ...
- Struts 2.x异常:Unable to load configuration..../WEB-INF/lib/struts2-convention-plugin-2.1.6.jar!/struts-plugin.xml:30:119
Struts 2.x异常:Unable to load configuration..../WEB-INF/lib/struts2-convention-plugin-2.1.6.jar!/strut ...
- [AngularJS] ui-router: named views
The ui-router library for AngularJS provides the ability to name views within your application. This ...
- Android Configuration change属性
问题:横竖屏切换时Activity的生命周期? 答案: 1.不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次 2 ...