zoukankan      html  css  js  c++  java
  • 【收藏】C#中得到程序当前工作目录和执行目录的一些方法

    原文:

    http://www.cnblogs.com/NalrA/archive/2009/06/15/1503568.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\,有时不一定返回什么东东,我也搞不懂了。
    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的比较常用,下面的是验证测试代码:


     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();
  • 相关阅读:
    SpringMVC,3种不同的URL路由配置方法(这根本不是一个小问题)
    PHP在Windows下安装配置第一步
    跟我一起学extjs5(18--模块的新增、改动、删除操作)
    html image -- data:image/png;base64
    oc66--代理模式应用2
    oc65--协议应用1,接口.做数据类型限定
    oc64--协议2@protocol
    oc63--协议@protocol1
    oc62--block1
    oc61--block
  • 原文地址:https://www.cnblogs.com/81/p/2313605.html
Copyright © 2011-2022 走看看