c#中如何取得应用程序运行路径

news/2025/6/19 16:37:01

string str1 =Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。 

  string str2=Environment.CurrentDirectory;//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。

  //备注 按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:/”)。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如“C:/mySubDirectory”)。

  string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。

  string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。

  string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

  string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

  string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。

  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/,有时不一定返回什么东东,我也搞不懂了。

  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一样。 

  =========================

  对于.net。有如下几种方式取得应用程序路径。

  1、Server.MapPath

  2、System.Windows.Forms.StartupPath

  3、Type.Assembly.Location

  方法2可以应用于控制台应用程序,WinForm应用程序,Windows服务,方法1可以应用于Web应用程序,方法3都可以应用。

  但方法3是加载应用程序的路径。如果是Web应用程序,取得的路径是C:/WINDOWS/Microsoft.NET/Framework/v1.1.4322/Temporary ASP.NET Files目录。所以Web项目还是使用Server.MapPath吧。否则建议使用方法2。如果自己新建类库。可以加入对System.Windows.Forms.StartupPath的引用后使用。

  ===================

  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

  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

  对于Windows程序 和Web 应用程序来说,他们运行的路径是不一样的,所以关键是判断当前运行的程序是哪种程序.于是我们可以使用如下的代码 

  

   

   string path = "";

  

   

   if (System.Environment.CurrentDirectory == AppDomain.CurrentDomain.BaseDirectory)//Windows应用程序则相等

  {

  path = AppDomain.CurrentDomain.BaseDirectory;

  }

  

   

   else

  {

  path = AppDomain.CurrentDomain.BaseDirectory + "Bin/";

  }

  这样如果我们写了一个类库,类库中用到了Assembly.LoadFrom,由于是通用类库,所以可能用到Windows程序中也可能用到Web中,那么用上面的代码就很方便了.

  ================================

  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”声明该引用;


https://dhexx.cn/news/show-242809.html

相关文章

JDBC应用:jdbc程序访问mysql如何加载jdbc驱动

1.若不用开发工具仅仅是一个操作数据库的java文件,在dos下执行,那么需要将jdbc驱动的jar包添加在环境变量中系统环境变量的classpath中。 具体做法:将mysql-connector-java-5.1.7-bin.jar放在任一盘中,例如放在D盘的JdbcDriver文…

cocos2d-x -- CCSAXParser笔记

CCSAXParser是异步解析的,而自带的libxml2库是同步解析的。 使用CCSAXParser,就要继承它,并实现startElement / endElement / textHandler三个函数,如 void HelloWorld::startElement(void *ctx, const char *name, const char *…

[ AWS | Migration ] 传统系统如何迁移到云(一):数据库迁移到云

本文主要介绍,将既有的传统环境,利用AWS Migration Service进行迁移,本文重点的讲解如何进行数据库迁移。 文章目录1. 设置网络配置安全组2. 创建目标数据库数据库迁移为目标数据库创建子网组创建目标数据库3. 创建复制实例创建复制子网组创建…

WPF绑定XML ListBox显示

Window1.xaml 前台代码 <Window x:Class"WpfApplication1.Window1" xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x"http://schemas.microsoft.com/winfx/2006/xaml" Title"WPFDataBindingXML&qu…

js计算日期相差天数的问题

js计算日期相差天数的问题如何通过JS计算两个日期相差天数&#xff08;月份同理&#xff09;&#xff1a; var s1 "2007-01-01".replace(/-/g, "/"); var s2 "2007-12-31".replace(/-/g, "/"); d1 new Date(s1); d2 new Date(s2);…

cocos2d-x 配置 tinyxml 库

1.去tinyxml官网下载压缩包&#xff0c;解压后&#xff0c;把.h和.cpp文件复制到你的具体项目的class类下&#xff0c;然后添加到项目中。&#xff08;可以单独起一个tinyxml文件夹存放这些文件&#xff09; 2.在要用到解析库的地方&#xff0c;添加头文件&#xff1a;include…

CSDN博客 - Markdown:怎样直接复制粘贴全文/复制活动贴模板

重要&#xff1a;在CSDN里文章抄来抄去的行为实属low逼&#xff01; 如果你看见好文章想转载到自己博客里&#xff0c;但是又不知道怎么复制&#xff0c;希望这个文章对你有帮助&#xff0c;该方法不仅针对CSDN&#xff0c;其他社区或者博客也一样。 如果想转载文章&#xff0…

通过内存映像文件共享一组对象

通过内存映像文件共享一组对象&#xff08;主程序和动态库或者各进程之间共享一组对象&#xff09; //单元设计: 陈新光(CXG) //设计时间: 2009-10-8 16:51:40 //单元功用: 封装内存映射操作 unit uMap; interface uses Windows, SysUtils, Dialogs; procedure SetMappedObj(co…

C#代码 数据库连接配置界面

以前做Winform程序&#xff0c;居然专门做一个界面来配置连接字符串。今天无意中发现&#xff1a;竟然可以直接调用VS.net2005中的配置界面来处理。 使用方法也及其简便&#xff1a; 一、添加引用 C:/Program Files/Microsoft Visual Studio 8/Common7/IDE/Microsoft.Data.Conn…

js 获取当前时间格式怎么转换?

查询操作&#xff1a; <td width"240"> <c:Calendar id"search_date" label"开始日期" width"120" labelWidth"120" labelAlign"cen…