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中,那么用上面的代码就很方便了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/407910.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

requires表达式 ---C++ 20 模板

requires表达式 —C 20 模板 requires还可以接一个表达式,该表达式也是一个纯右值表达式,表达式为true时满足约束条件,false则不满足约束条件 requires表达式的判定标准:对requires表达式进行模板实参的替换,如果替换之后出现无效类型,或者违反约束条件,则值为false,反之为tr…

读古诗系列--(两首)题都城南庄/江楼感旧

题都城南庄 唐 崔护去年今日此门中, 人面桃花相映红。 人面不知何处去, 桃花依旧笑春风。 江楼感旧 唐 赵嘏 独上江楼思渺然,月光如水水如天。 同来望月人何在?风景依稀似去年。前为课本所学,皆知&#xff…

react(86)--列表项控制选中

if (newarr.length 0) {message.error(请选中列表内容);return false;}

CheckBoxList 全选(jquery版本)

function selectedAll(allselect, obj) {$("#"obj.id" input:checkbox").each(function () {if (allselect.checked)$(this).attr("checked", true);else {$(this).attr("checked", false);}});} 调用 <input id"allCheck&quo…

如何输出源文件的标题和目前执行行的行数

环境&#xff1a;VC6.0 C版本&#xff1a; #include<iostream>using namespace std; void main(){ int line __LINE__; //注意&#xff1a;LINE前后分别是两个下划线“-”(半角状态下) char * file __FILE__; cout<<line<<endl; cout<<file<&…

类型萃取类型比较 Type-Traits Librarytype comparisons --- C++20

类型萃取:类型比较 Type-Traits Library:type comparisons — C20 不涉及runtime只在编译期 Comparing types 类型比较 C11 支持三种类型: is_base_of<Base, Derived>is_convertible<From, To>is_same<T, U> C20新加了几种: is_pointer_interconvertibl…

SBO部分SQL查询奉献

产品生产主计划SELECT T0.MsnCode AS 计划单号, T1.ItemCode, T2.itemname AS 产品名称,T1.Quantity as 生产数量, T2.onhand,T2.onorder as 已下单,T1.StartDate, T1.EndDate, T1.BaseDocNum as 订单号,T1.BaseDue as 完工日期,T1.ParentCode as 产品名 FROM OMSN T0 INNER JO…

react(87)--控制禁用disabled

<Form.Item label"活动详情" required validateStatus{textErr} help{textMsg}><BaseRichTextdisabledheight{360}editorState{introeditorState}handleEditorChange{this.handleEditorChange}/></Form.Item>

奇葩面试经历一

大清早坐车到**公司坐等面试&#xff0c;前台MM热情招待把哥们安置到一个小会议室&#xff0c;顺便给了一张资料填写单。 急吼吼的把资料填写完啦后&#xff0c;MM说让俺们在会议室稍等一下。不久&#xff0c;带哥来到一办公司门口&#xff0c;打眼一瞧&#xff0c;回到&#x…

判断一个无符号整数是不是2的幂

C版本&#xff1a; #include<stdio.h> //原理&#xff1a;2的幂的二进制位中有且只有一位是1int Is2Power(unsigned int d){ int i sizeof(d) << 3; //这里i得到d所占的位数(bits)&#xff0c;因为要对d的每一位作判断 unsigned int v; while(i > 0) { v 1…

童趣

1.豆豆的外婆买了一只母鸡&#xff0c;打算杀了吃。豆豆不允许杀它&#xff0c;外婆就把鸡养在院子里。每天早上&#xff0c;外婆在院子里干家务&#xff0c;豆豆就坐在小板凳上&#xff0c;手里拿了本小画书&#xff0c;绘声绘色地给母鸡讲故事。豆豆给母鸡起的小名是“小鸭”…

PROJECT #1 - BUFFER POOL [CMU 15-445645]笔记

PROJECT #1 - BUFFER POOL 15-445/645笔记 因为在主存中储存所有块是不可能的&#xff0c;我们需要管理主存中用于存储块的可用空间的分配 。缓冲区是主存中用于存储磁盘数据块拷贝的那部分。 缓冲区管理器 ​ 当数据库系统中的程序需要磁盘上的块时&#xff0c;它向缓冲区管…

判断一个无符号整数是不是2的n次幂的幂

C版本&#xff1a; #include<stdio.h> unsigned int f2(int n); //判断无符号整数d是不是[2的n次幂]的幂,并指出该数[d]是[2的n次幂]的多少(e)次幂//原理&#xff1a;2的n次幂的幂的二进制位中有且只有一位是1,且1后面刚好是n个0为一组int IsPowerof2Power(unsigned in…

Eclipse换常用的快捷键

还是喜欢ctrltab键来切换窗口&#xff0c;ctrlf6实在不好使。 修改方法&#xff1a;在eclipse中Window -> Perferences -> General -> Keys -> 查找“Next Editor”&#xff0c;把Ctrl F6修改为Ctrl Tab 转载于:https://www.cnblogs.com/HD/p/3654197.html

MS SQL入门基础:系统数据库

SQL Server 2000 有四个系统数据库&#xff08;System Databases&#xff09;&#xff1a;Master、Model、Msdb、Tempdb。这些系统数据库的文件存储在Microsoft SQL Server&#xff08;默认安装目录&#xff09;的MSSQL子目录的Data 文件夹中。各系统数据库所包含的文件如表6-1…

C++20 std::jthread

C20 std::jthread std::jthread 表示 joining thread , 与C11里面的std::thread不同std::jthread 自动join, 并且可以被外部终止 自动join std::thread #include <iostream> #include <thread> using namespace std;int main(int argc, char* argv[]) {std::cou…

WebRTC音视频引擎研究(2)--VoiceEngine音频编解码器数据结构以及参数设置

1、VoiceEngine Codec数据结构 WebRTC中&#xff0c;用一个结构体struct CodecInst表示特定的音频编解码器对象&#xff1a; [cpp] view plaincopy struct CodecInst { int pltype; //payload type负载类型 char plname[32]; //payload name负载名称&#xff0…

.NET资源收集

.NET开发资源精华收集与共享 .Net General微软.Net中文主页 .NET Framework开发中心 微软.Net Framework中文产品支持中心 微软.Net Framework中文新闻组(Web) 微软VB.Net中文新闻组(Web) 博客园 .NET 2.0 专题 孟宪会之精彩世界 微软.NET俱乐部 MSDN中文网络广播 AspxBoy教程网…