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

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

问题描述

在这里得到了一个恼人的问题。我有一个NHibernate的/表单应用程序,我通过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 files\vs 9.0\common7\ide,然后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 files\vs 9.0\common7\ide, 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目录(将工作)。我想一个解决方案,得到的解决方案目录,而当前目录是common7\ide。一些能够让别人看到我的形式在设计上的一个新鲜结帐到任意目录中的任意机器上。不,我不会加载代码的控制。我的控制之内如此多的控件,它是一场噩梦排队一切行动离不开它。

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 common7\ide. 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.

我试过提出,在解决方案目录中的文件预先生成事件它,当然,我怎么能发现从common7\ide?所有项目文件需要,因为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 common7\ide? 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&安培;页= 164 。由于我使用Visual Studio 2010,我做了一些小的改动。你必须引用EnvDTE和EnvDTE100(EnvDTE90的VS2008)

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天全站免登陆