获取当前文件夹路径 [英] Get current folder path

查看:273
本文介绍了获取当前文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建转换文件的程序。我希望用户能够放置在任何目录下的可执行文件,并在执行程序时(在.exe文件双击)我想要的程序来处理的所有当前文件夹中的文件,其中的EXE文件存在。程序如何确定它当前正在执行的路径?

I want to create a program that converts files. I would like the user to be able to place the executable file in any directory, and when executing that program (double-clicking on the .exe) I want the program to process all the files within the current folder where the exe file exists. How can the program determine the path in which it is currently executing?

我试过 System.Windows.Forms.Application.StartupPath 但是,这似乎是走错了路。

I tried System.Windows.Forms.Application.StartupPath but that seems to be the wrong way.

任何想法?

推荐答案

您应该的不可以使用 Directory.GetCurrentDirectory()在你的情况,作为当前目录可能从执行文件夹不同,尤其是当你通过一个快捷方式执行程序。

You should not use Directory.GetCurrentDirectory() in your case, as the current directory may differ from the execution folder, especially when you execute the program through a shortcut.

这是更好地使用 Path.GetDirectoryName(Assembly.GetExecutingAssembly()位置。); 你的目的。这将返回那里的当前执行的程序集所在的路径。

It's better to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); for your purpose. This returns the pathname where the currently executing assembly resides.

虽然我的建议的方法,您可以执行的程序集,进入组装或任何其他加载的程序集区分开来,为的 Soner格尼尔的他回答说,

While my suggested approach allows you to differentiate between the executing assembly, the entry assembly or any other loaded assembly, as Soner Gönül said in his answer,

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

也可能是足够的。这将等于

may also be sufficient. This would be equal to

System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

这篇关于获取当前文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆