如何找到我的程序目录 [英] How to find my programs directory

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

问题描述

我想知道如何找到程序所在的目录?就像说它在桌面上一样.如何获取程序本身以查找其所在的目录并设置字符串或设置为目录?

Hi I would like to know how to find the directory that a program is in? Like say it is on the desktop. How do I get the program itself to find the directory that it is in and set a string or setting as the directory?

推荐答案

这是执行此操作的方法之一不需要任何专门的程序集引用(例如使用System.Windows.Forms.Application的方法),并且无论应用程序如何托管,始终可以正常工作:

C#:
This is one of the methods which does not require any specialized assembly references (such as the method using System.Windows.Forms.Application) and always works correctly, no matter how application is hosted:

C#:
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
string executableDirectory = System.IO.Path.GetDirectoryName(location);


VB.NET:


VB.NET:

Dim location As String = System.Reflection.Assembly.GetEntryAssembly.Location
Dim executableDirectory As String = System.IO.Path.GetDirectoryName(location)



它的作用是:它找到 entry程序集的主可执行模块的位置,该模块包含您的应用程序 entry point ("c1>).当然,您可以找到任何其他程序集的位置,例如,调用程序集.请参阅:
http://msdn.microsoft.com/en-us/library/system.reflection. assembly.aspx [^ ].

重要的是要了解,找到此目录的方式有所不同,但是其中一些方式取决于应用程序的托管方式(在Windows服务或在调试器下执行时,我不会返回正确的结果),但是我在这里显示的方法始终可以正常工作.

—SA



What it does is: it finds the location of the main executable module of the entry assembly, the one containing your application entry point ("Main"). Of course, you can find location of any other assembly, for example, the calling assembly. Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^].

It''s important to understand that there are some different ways of finding this directory, but some of them depends on how application is hosted (my not return correct results in case of Windows service or when are executed under the debugger), but the method I show here always works correctly.

—SA


嗨.
用这个鳕鱼

Hi .
use this cod

Application.StartupPath.ToString



像这样,在form_load上:



such as this , on form_load :

MsgBox(Application.StartupPath.ToString, MsgBoxStyle.OkOnly, "Application Directory is")


回答OP在问题注释中提出的其他问题:

用户拥有与其个人资料相关联的目录;还有一个针对所有用户"的类似目录.这全称为特殊目录"(文件夹").使用System.Environment.GetFolderPath:
获取它们 http://msdn.microsoft.com/en-us/library/14tx8hby.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.environment. specialfolder.aspx [ ^ ].

当前目录System.Environment.CurrentDirectory完全不同:
http://msdn.microsoft.com/en-us/library/system.environment. currentdirectory.aspx [ ^ ].

重要的是要理解,即使您的应用程序永远不会更改此属性值,也永远不要依赖任何特定的当前目录.这完全取决于用户如何启动应用程序.绝对可以是任何东西.默认情况下,它仅取决于父进程的当前目录(通常,某些文件管理器,例如Windows资源管理器),还取决于使用的是.LNK文件的参数.这就是为什么此属性在好的软件中不经常使用的原因.通常,应用程序(尤其是控制台应用程序)使用当前目录中的文件(无论该文件是什么),但都使用与该目录无关的方式,只需使用用户提供的相对路径名即可.命令行等).

我认为,现在您已经在运行时很好地掌握了与您的应用程序相关的目录的知识. :-)

—SA
Answering the additional question asked by OP in the comment to the question:

The user have directories associated with their profiles; and also there is a similar directory for "all users". This is all called "special directories" ("folders"). Get them using System.Environment.GetFolderPath:
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^].

The current directory, System.Environment.CurrentDirectory is something completely different:
http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx[^].

It''s important to understand that you should never rely on any particular current directory even it your application never change this property value. It entirely depends on how the user starts the application. It can be absolutely anything. By default, it simply depends on the current directory of the parent process (quite typically, some file manager, such as Windows Explorer) and also on the parameter of a .LNK file is one is used. That''s why this property is not often used in good software. It''s more typical that an application (especially a console application) uses the files from the current directory, whatever it is, but in a way agnostic to what is that directory, simply by using relative path names supplied by user (in a command line or whatever).

I think, now you are pretty well armed with the knowledge on directories relevant to your application during run time. :-)

—SA


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

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