如何在winforms、.net中获取设计时的应用程序路径?
如果在引用的dll中使用Application.StartupPath,则该路径指向IDE的路径。
有没有办法获取实际应用程序的路径?
需要明确的是,这是在设计时。
ETA:我已经发布了下面的解决方案:
ETA2:
因为它是相关的,所以我想我应该发布另一个有用的设计时服务的片段。与下面的解决方案类似,此示例适用于 UITypeEditor:
Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
Dim typeDiscovery As ITypeDiscoveryService = TryCast(provider.GetService(GetType(ITypeDiscoveryService)), ITypeDiscoveryService)
Dim types As ICollection = typeDiscovery.GetTypes(GetType(MyType), False)
End Function
类型将包含从 MyType 派生的所有类型。将第二个参数更改为 True 以排除搜索 GAC。 传递 Nothing 作为第一个参数以获取所有类型的列表。
If you use Application.StartupPath in a referenced dll, the path points to the path of the IDE.
Is there anyway to get the path of the actual application?
Just to be clear, this is at design time.
ETA: I've posted the solution below:
ETA2:
Because it's related, I thought i'd post a snippet of another useful design-time service. Like the solution below, this example is for a UITypeEditor:
Public Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
Dim typeDiscovery As ITypeDiscoveryService = TryCast(provider.GetService(GetType(ITypeDiscoveryService)), ITypeDiscoveryService)
Dim types As ICollection = typeDiscovery.GetTypes(GetType(MyType), False)
End Function
types will contain all types derived from MyType. Change the second parameter to True to exclude searching the GAC.
Pass Nothing as the first parameter to get a list of all types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检测 C# windows 表单应用程序的启动路径
我如何获取该应用程序控制台应用程序中 C# 的路径
Detecting the startup path of a C# windows Form Application
How can i get the application path in C# in a console app
以下是如何从 UITypeEditor 执行此操作。
ETA:原始代码有一个额外的不需要的步骤。我忘记了我们有一个服务提供商,所以不需要查看该网站。简化后的代码为:
原始代码:
此解决方案基于 如何:访问设计时服务,您可以在其中找到大量信息。
Here's how to do it from a UITypeEditor.
ETA: The original code has an extra unneeded step. I forgot we have a serviceprovider, so no need to look at the site. The streamlined code is:
Original Code:
This solution is based on code at How to: Access Design-Time Services, where you'll find a wealth of information.
你不能在设计时做到这一点!在运行时,即当您构建应用程序并运行它时,或者在 VS 中按 F5 或单击绿色箭头时,您可以。为什么你想在设计时知道?这是无关紧要的,因为可执行文件及其相关的 DLL 并未真正加载到内存中并执行,此外,如果对代码进行更改,则必须重新构建整个项目。
希望这有帮助,
此致,
汤姆.
You cannot do it at design-time! At runtime you can, that is, when you build the application and run it, or hitting F5 within VS or clicking on the green arrow. Why would you want to know at design-time? It is irrelevant as the executable and its related DLL's are not really loaded into memory and executed, plus, if a change was made to the code, the whole project would have to be rebuilt again.
Hope this helps,
Best regards,
Tom.