C#文件路径操作总结【转】
http://www.cnblogs.com/zhoufoxcn/archive/2006/10/24/2515874.html
一、获取当前文件的路径
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
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#文件路径操作总结【转】的更多相关文章
- OC NSFileManager(文件路径操作)
OC NSFileManager(文件路径操作) 初始化 NSFileManager * fm = [NSFileManager defaultManager]; 获取当前目录 [fm current ...
- Go path/filepath文件路径操作
本文:https://books.studygolang.com/The-Golang-Standard-Library-by-Example/chapter06/06.2.html path:htt ...
- python文件名和文件路径操作
Readme: 在日常工作中,我们常常涉及到有关文件名和文件路径的操作,在python里的os标准模块为我们提供了文件操作的各类函数,本文将分别介绍"获得当前路径""获得 ...
- 【Python】python文件名和文件路径操作
Readme: 在日常工作中,我们常常涉及到有关文件名和文件路径的操作,在python里的os标准模块为我们提供了文件操作的各类函数,本文将分别介绍“获得当前路径”“获得当前路径下的所有文件和文件夹, ...
- C#文件路径操作总结
一.获取当前文件的路径 1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName 获取模块的完整路径,包括 ...
- Python文件路径操作
print(os.environ.get('HOME')) # 打印`HOME`这个环境变量 /Users/<> file_path = os.environ.get('HOME') + ...
- 使用File类、StreamRead和StreamWrite读写数据、以及Path类操作文件路径和Directory
1.File类的概念: File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件. File类方法的参量 ...
- SQL2012还原数据库操作在本地服务器上操作和用别的电脑远程连接到服务器进行操作的文件路径差异
在数据库服务器上想还原一个数据库到某个备份文件时期的,服务器的数据库文件本身是保存在 D:\DEVDB目录 通过开发电脑上的MS manager来连接数据库服务器操作还原 虽发现文件卡项上,原始文件名 ...
- python,os操作文件,文件路径(上一级目录)
python获取文件上一级目录:取文件所在目录的上一级目录 os.path.abspath(os.path.join(os.path.dirname('settings.py'),os.path.pa ...
随机推荐
- Java 程序动态替换 docx 模板中定制值的实现例子
项目系统中打印功能,导出 word 文档功能是挺常用的,本文介绍自定文档模板,程序实现模板内容中值替代的功能. 模板文件 template.docx 执行 main public static v ...
- tomcat6-输入输出buffer设计
之前写的一个ppt 搬到博客来
- you build it,you run it
this article is almostly about a book named Migrating_to_Microservices_Databases, and it's just the ...
- curl post 用json方式
if(!function_exists('tps_curl_post3')){ function tps_curl_post3($url, $postData) { $postData = json_ ...
- linux磁盘问题排查
一.ls $>>ls /data* //查看有无input/output error报错 二.demsg $>>demsg|grep sd 问题盘结果: 三.iostat使用 ...
- [NOIP2009] $Hankson$ 的趣味题 (数论,gcd)
题目链接 Solution 此题,用到的结论都是比较浅显的,但是,我竟然没想到反过来枚举... 只有50分... 被自己蠢哭... 结论比较浅显: 1.对于两个正整数\(a\),\(b\),设 \(g ...
- SyntaxError: Non-UTF-8 code starting with '\xb4'...
需在开头指定编码格式,在在最开头添加如下代码: # -*- coding: gb2312 -*- 大功告成!
- 关于cookie使用的一些问题
保存cookie后提取出来发现字符串是被编码过的,需要decodeURIComponent进行下解码才可以 设置cookie setCookie(c_name, value, expiredays) ...
- QBXT 二月五号整理
给你一列数, 询问和最大的子串. N<=10^6 // N <=10^6 #include<cstdio> #include<iostream> using nam ...
- 转 手把手教你最简单的开源项目托管GitHub入门教程
传送门 自从google code关闭了下载服务了之后,GitHub作为了目前最好用的免费开源项目托管站点,众多开源项目都托管在github,其中不乏著名的播放器MPC-HC. 不习惯于英文的朋友,难 ...