如何操作目录位置 [英] How to manipulate directory location

查看:65
本文介绍了如何操作目录位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

很抱歉,如果我的标题不正确...我只想知道您将如何操作当前目录位置?这是场景:

假设我的项目位于c://myfiles/program/myproject
而我的exe文件位于

Hello guys,

I''m sorry if my title is not correct... I just want to know how will you be able to manipulate your current directory location? Here is the scenario:

Assuming that my project is located in c://myfiles/program/myproject
and my exe file is located in

c://myfiles/program/myproject/bin/debug/myexe.exe



所以我想做的就是获取现有的项目位置,它只是



So what I want to do is I want to get the existing project location which is only the

c://myfiles/program/myproject

而不是

c://myfiles/program/myproject/bin/debug



我使用此代码来获取当前项目位置



I used this code to get the current project location

string startupPath = System.IO.Directory.GetCurrentDirectory()

,但它仅返回此

but it only returns this

c://myfiles/program/myproject/bin/debug

我想备份2个目录,这样我可以转到

I want to back 2 directories that way I can go to

c://myfiles/program/myproject

...

我怎么做?

请帮忙.

在此先感谢

...

How do i do that?

Please help.

Thanks in advance

推荐答案

请勿混淆两个完全不同的目录:工作目录(这是您找到的目录)和可执行目录.工作目录对于简单的应用程序很有用,但是它并不取决于可执行文件的位置,它完全取决于用户在哪里启动应用程序,并且用户可以从任何目录启动.

相反,可执行文件目录具有某些位置,具体取决于可执行文件的位置.有几种不同的查找方法,但是从结果的角度来看,它们取决于应用程序的托管方式,因此其中某些方法并不可靠.特别是,在Windows Studio或Service Controller(对于Windows Service应用程序)下运行该应用程序会引起问题.这是找到此目录的最确定的方法:

Do not mix up two completely different directories: working directory (this is the one you found) and executable directory. Working directory is good for simple applications, but it does not depend on where your executable files are, it totally depends on where the users starts the application, and the user can start if from any directory.

In contrast, executable directory has certain location depending where your executable files are. There are several different ways to find it, but some of them are not reliable in that sense that the results depend on how the application is hosted. In particular, running the application under Windows Studio or Service Controller (for Windows Service applications) cause problems. This is the most certain method to find this directory:

string exeDirectory =
    System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetEntryAssembly().Location);



如果找到当前正在运行的应用程序(.EXE文件)的入口程序集的位置,更确切地说,是其主要可执行模块的位置,则此方法也不依赖于运行此代码的程序集.

—SA



This method also does not depend on the assembly running this code, if finds location of the entry assembly of the currently running application (.EXE file), more exactly, location of its main executable module.

—SA




如果可以的话请尝试...

Hi,

Try this if could help...

string startupPath = AppDomain.CurrentDomain.BaseDirectory.ToString()




问候,

代数




Regards,

Algem


这篇关于如何操作目录位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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