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

以上出处:九头龙的Blog

 

若在类库文件中获取以上各种信息则会产生使用类库的应用程序的目录信息.Highlight的比较常用,下面的是验证测试代码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 Process process = Process.GetCurrentProcess();
 2             _fileName = "f://log.txt";
 3             _sw = new StreamWriter(_fileName);
 4             _sw.WriteLine("MainModule is :"+process.MainModule.FileName);
 5             _sw.WriteLine("Environment.CurrentDirectory is :"+Environment.CurrentDirectory);
 6             _sw.WriteLine("System.IO.Directory is :" + System.IO.Directory.GetCurrentDirectory());
 7             _sw.WriteLine("System.AppDomain.CurrentDomain.BaseDirectory is :" + System.AppDomain.CurrentDomain.BaseDirectory);
 8             _sw.WriteLine("System.AppDomain.CurrentDomain.DynamicDirectory is :" + System.AppDomain.CurrentDomain.DynamicDirectory);
 9             _sw.WriteLine("System.AppDomain.CurrentDomain.RelativeSearchPath is :" + System.AppDomain.CurrentDomain.RelativeSearchPath); 
10             _sw.WriteLine("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase is : " + System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase);
11             _sw.WriteLine("System.Windows.Forms.Application.ExecutablePath is : " + System.Windows.Forms.Application.ExecutablePath);
12             _sw.Flush();

 

 

转载于:https://www.cnblogs.com/NalrA/archive/2009/06/15/1503568.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值