如何在设计时获取项目路径
我使用一个组件(System.ComponentModel.Component),我想获取我的项目的应用程序路径,以便在其中创建一个文件。
谢谢弗洛里安
I use a component (System.ComponentModel.Component) and I want to get the application path of my project in order to create a file within it.
Thx
Florian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
唯一对我来说(一致)有效的事情是获取EnvDTE.DTE(从EditValue()获得的IServiceProvider),即:
当我尝试使用Assembly.GetXAssembly时,我得到了VS在设计时使用的临时路径。
The only thing that seemed to work (consistently) for me was obtaining EnvDTE.DTE (from the IServiceProvider you get from EditValue()), i.e.:
When I tried using Assembly.GetXAssembly, I got the temporary path VS uses in design-time.
只需调用 GetMyPath 即可,其定义为
Just call GetMyPath which is defined as
使用AppDomain.CurrentDomain.BaseDirectory。
Use AppDomain.CurrentDomain.BaseDirectory.
这有效吗?
(“CallingAssembly”是因为你可以把检索执行路径的方法放到服务层(程序集))
Does this work?
("CallingAssembly" because you can put the method to retrieve the execution path into the service layer (assembly))
看一下:编译时的项目(bin)文件夹路径?
通过这种技术,您可以“构建”(一次),然后使用生成的文件在设计时获取项目位置。这将独立于工作空间工作。
Take a look at: Project (bin) folder path at compile time?
With this technique you could "Build" (once) and after that use the generated file to get the project location at DesignTime. This would work workspace independent.
我认为罗伯特几乎是对的。
这似乎有效:
I think Robert is almost right.
This seems to work:
我认为这是最好的解决方案,因为您不必添加任何比“Using System.IO”更多的库:)
I think this is the best solution, because you dont have to add any library more than "Using System.IO" :)
我做了一个简单的测试(只是作为临时设计,我添加了一个字符串属性来保存当前目录值)(我不描述数据绑定的所有过程。请参阅我的一些关于设计的帖子时间/运行时绑定)
在主窗口 ViewModel 类的空构造函数中(专用于设计时绑定)
在 mainwindow.xaml 文件中我添加了一个文本框来显示结果(不介意网格行值)
并且我进入 VS 的设计视图(如 Florian 的评论所述,但 4 年后具有更新的值):
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE
I did a simple test ( just as a temporary design, I added a string property to hold the current directory value ) ( I do not describe the all process of data binding. See some of my post about design time / run-time binding )
In the empty constructor of the main window ViewModel class ( dedicated to design time binding )
In the mainwindow.xaml file I added a textbox to display the result ( don't mind the grid row value )
And I got in the design view of VS ( like Florian's comment stated , but with a newer value 4 years after):
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE
如果您正在谈论 WPF 设计器,请使用“Context”属性/类型
详细信息:-
在设计时,您有 modelItem 的实例(我假设它,您知道)如果没有,那么您可以在
DesignAdorner 类中的Activate 方法的 Override 实现 //
中实例化它现在您可以使用以下单行代码访问当前应用程序路径
让我知道,如果这对你没有帮助。
If you are talking about WPF designer, please use "Context" property/type
Details:-
In Design time you have instance of modelItem (I assume it, you know it) if not then you can instantiate it in Override implementation of Activate method
// in DesignAdorner class
Now you can access the current application path using following single line code
Let me know, if it does not help you.
添加对 .NetFrameWork 4.5 程序集中的 envdte80 的引用。
Add a reference to envdte80 which is in .NetFrameWork 4.5 Assemblies.