如何在代码中获取 C# (VS 2008) 中的解决方案目录? [英] How do you get the solution directory in C# (VS 2008) in code?

查看:23
本文介绍了如何在代码中获取 C# (VS 2008) 中的解决方案目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个烦人的问题.我有一个 NHibernate/Forms 应用程序,我正在通过 SVN 工作.我制作了一些我自己的控件,但是当我将这些控件(或查看一些我已经拖放的表单编辑器)拖放到我的其他控件上时,Visual Studio 决定它需要执行我编写的一些代码,包括寻找 hibernate.cfg.xml 的部分.

Got an annoying problem here. I've got an NHibernate/Forms application I'm working through SVN. I made some of my own controls, but when I drag and drop those (or view some form editors where I have already dragged and dropped) onto some of my other controls, Visual studio decides it needs to execute some of the code I wrote, including the part that looks for hibernate.cfg.xml.

我不知道为什么会这样,但是(有时!)当它在我的表单加载或拖放期间执行代码时,它会将当前目录切换到 C:program filesvs 9.0common7ide,然后nhibernate 抛出一个异常,它无法找到 hibernate.cfg.xml,因为我在相对路径中搜索.

I have no idea why this is, but (sometimes!) when it executes the code during my form load or drag and drop it switches the current directory to C:program filesvs 9.0common7ide, and then nhibernate throws an exception that it can't find hibernate.cfg.xml, because I'm searching for that in a relative path.

现在,我不想硬编码 hibernate.cfg.xml 的位置,或者只是将 hibernate.cfg.xml 复制到 ide 目录(这将起作用).我想要一个获取解决方案目录的解决方案,而当前目录是 common7ide.可以让某人在对任意机器上的任意目录进行全新结帐时查看我在设计器中的表单的东西.不,我不打算在代码中加载控件.我在控件中有很多控件,如果没有它,将所有内容都排列起来简直是一场噩梦.

Now, I don't want to hard code the location of hibernate.cfg.xml, or just copy hibernate.cfg.xml to the ide directory (which will work). I want a solution that gets the solutions directory while the current directory is common7ide. Something that will let someone view my forms in the designer on a fresh checkout to an arbitrary directory on an arbitrary machine. And no, I'm not about to load the controls in code. I have so many controls within controls that it is a nightmare to line everything up without it.

我尝试了一个预构建事件,该事件创建了一个包含解决方案目录的文件,但当然我如何从 common7ide 中找到它?由于 svn,所有项目文件都需要在解决方案目录中.

I tried a pre build event that made a file that has the solution directory in it, but of course how can I find that from common7ide? All the projects files need to be in the solution directory because of svn.

感谢你们的帮助,我已经白费了几个小时.

Thanks for your help guys, I've already spent a few hours fiddling with this in vain.

更新:我将 hibernate.cfg 设置为嵌入式资源.对于每个配置,我只是简单地创建一个新的构建配置、调试、发布、XYZ.在大多数情况下,我建议嵌入运行程序所依赖的任何文件.它使构建安装程序变得更加简单.

UPDATE: I set hibernate.cfg as an embedded resource. For each configuration I just simply make a new build configuration, debug, release, XYZ. In most cases I'd recommend embedding any files you depend on to run the program. It makes it much simpler to build an installer.

推荐答案

这可能有点晚了,但我在 http://www.tek-tips.com/viewthread.cfm?qid=1226891&page=164.因为我使用的是 Visual Studio 2010,所以我做了一些小改动.你必须参考EnvDTE和EnvDTE100(VS2008的EnvDTE90),

This is probably coming a little bit late, but I've found a solution at http://www.tek-tips.com/viewthread.cfm?qid=1226891&page=164. Since I am using Visual Studio 2010, I made a few minor changes. You have to reference the EnvDTE and EnvDTE100 (EnvDTE90 for VS2008),

string solutionDirectory = ((EnvDTE.DTE)System.Runtime
                                              .InteropServices
                                              .Marshal
                                              .GetActiveObject("VisualStudio.DTE.10.0"))
                                   .Solution
                                   .FullName;
solutionDirectory = System.IO.Path.GetDirectoryName(solutionDirectory);

当然我用的是VisualStudio.DTE.10.0,你应该用VisualStudio.DTE.9.0.

Of course I used VisualStudio.DTE.10.0, you should probably use VisualStudio.DTE.9.0.

祝你好运!

这篇关于如何在代码中获取 C# (VS 2008) 中的解决方案目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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