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();
  • 相关阅读:
    Linux下动态库(.so)和静态库(.a) 的区别
    CTS、CLS、CLR
    ASP.NET简介及网页基础知识
    ASP.NET MVC中ActionResult的不同返回方式
    ADO.NET中的数据库帮助类
    ASP.NET MVC 方法View返回的两种方式
    使用win10 IIS 发布局域网网站
    ASP.NET MVC 给Action的参数赋值的方式
    ASP.NET MVC简单流程解释(传值方式)
    ASP.NET MVC 简介(附VS2019和VSCode版示例)
  • 原文地址:https://www.cnblogs.com/81/p/2313605.html
Copyright © 2011-2022 走看看