一、获取当前文件的路径
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");
System.Environment.SystemDirectory ;C:/windows/system32目录
最 后贴出我进行上面操作获得的变量值,事先说明,本人是编写了一个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”:获取当前应用程序所在目录的路径,最后不包含“\”;
C#获取路径方法_IT/计算机_专业资料。C#获取路径方法 自己整理出来的。非常全面.本人在项目中通过该文档的学习,项目更加的清晰。在项目中 得心应手/...
1、Server.MapPath 2、System.Windows.Forms.StartupPath 3、Type.Assembly.Location C#获取当前路径方法2可以应用于控制台应用程序, WinForm 应用程序, Windows 服务,...
C# 获取系统路径方法_电脑基础知识_IT/计算机_专业资料。C# 获取系统路径的方法返回“我的文档”路径字符串 Environment.GetFolderPath(Environment.SpecialFolder.Persona...
C#获取当前路径 string str1 =Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。 string str2=Environment.CurrentDirectory;//...
C#文件路径的获取_电脑基础知识_IT/计算机_专业资料。asp.net C#网站项目 获取物理路径、虚拟路径的方法当前项目的物理路径嘛: strPath = this.Server.MapPath(Reque...
C#获取项目程序路径的方法 //获取当前进程的完整路径,包含文件名(进程名)。 string str = this.GetType().Assembly.Location; result: X:\xxx\xxx\xxx.exe (...
如要投诉违规内容,请到百度文库投诉中心;如要提出功能问题或意见建议,请点击此处进行反馈。 加入阅读会员!获取下载券 c#获取路径 C#中获取路径名的各种方法C#中获...
C#中获取路径方法及代码_IT/计算机_专业资料。C#中获取路径方法及代码 中获取路径方法及代码 string str1 =Process.GetCurrentProcess().MainModule.FileName;//可...
C# 获取文件路径 winform 获取文件路径 string str1 =Process.GetCurrentProcess().MainModule.FileName;//获得当 前执行的 exe 的文件名。 string str2=...
} C#获取项目程序路径的方法 1.asp.net webform 用“Request.PhysicalApplicationPath 获取站点所在虚拟目录的物理路径,最后包 含“\”; 2.c# winform 用 A: ...
热门文档