C#读取文件

C#获取当前路径的方法

获取包含清单的已加载文件的路径或 UNC 位置
public static string sApplicationPath = Assembly.GetExecutingAssembly ( ).Location;
result: X:\xxx\xxx\xxx.dll (.dll文件所在的目录+.dll文件名)'


    //获取当前进程的完整路径,包含文件名(进程名)。
    string str = this.GetType ( ).Assembly.Location;
    //result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

    //获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
    string str = System.Diagnostics.Process.GetCurrentProcess ( ).MainModule.FileName;
    //result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

    //获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
    string str = System.Environment.CurrentDirectory;
    //result: X:\xxx\xxx (.exe文件所在的目录)

    //获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
    string str = System.AppDomain.CurrentDomain.BaseDirectory;
    //result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

    //获取和设置包含该应用程序的目录的名称。
    string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
    //result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

    //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
    string str = System.Windows.Forms.Application.StartupPath;
    //result: X:\xxx\xxx (.exe文件所在的目录)

    //获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
    string str = System.Windows.Forms.Application.ExecutablePath;
    //result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

    //获取应用程序的当前工作目录(不可靠)。
    string str = System.IO.Directory.GetCurrentDirectory ( );
    //result: X:\xxx\xxx (.exe文件所在的目录)

Get the path to the system directory

1.Environment.SystemDirectory Property:Gets a fully qualified path to the system directory

2.Environment.GetFolderPath Method:The parameter type accepted by this method is Environment.SpecialFolder,This method can obtain the path of a large number of system folders,such as my computer,desktop,system directory.etc

3.Path.GetTempPath

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,452评论 19 139
  • LambdaMART是一种state-of-art的Learning to rank算法,由微软在2010年提出[...
    天行剑阅读 17,071评论 3 52
  • 我们回到宾馆之后,老板娘一看到我们抬着的天若,突然趴在了桌子上,把头埋在了两个胳膊中。 我敲了敲桌子,说:“老板娘...
    玩美创游徐良阅读 3,709评论 0 0
  • 恭贺得民智能酒柜与乐福楼餐饮强势合作! 乐福楼餐饮,让顾客享受美味,享受健康。 不要以为这样就完了 美食怎能少了...
    那个女孩J阅读 2,523评论 0 0
  • 漂亮自信的妈妈也会带给孩子更足的自信心,这与经济条件好的家庭更容易培养出智商高的孩子是同样的道理。 最近给孩子增加...
    Tinko王阅读 1,189评论 0 0