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 som
It sounds like you just need to code a better path to your configuration file.
If you do something like this:
configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "\\PathToCFG");
you should not be messed up when Windows changes the current directory on you.
Edit: You may be running into an issue with the Visual Studio hosting process. Can you disable this? There is a check box under project properties \ debug.
I've finally figured this one out. This will work for any Visual Studio version, does not rely on EnvDTE, and solves the original problem presented here.
In your project settings, under "Build Events", add the following "Pre-build event command line":
echo $(SolutionDir) > ..\..\solutionpath.txt
Build the project once. The file will be created in your project root.
In solution explorer, click "Show All Files" and "Refresh"
Add solutionpath.txt to your solution
Right click solutionpath.txt, click properties. Change your build action to "Embedded Resource"
Use the following code to get your solution path.
string assemblyname = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
string path = "";
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(assemblyname + ".solutionpath.txt"))
{
using (var sr = new StreamReader(stream))
{
path = sr.ReadToEnd().Trim();
}
}
Because this is a pre-build event, the file does not need to exist before the build is started, so it is compatible with source-control and doesn't have any obvious issues.
Update: Unfortunately, i don't know how of a way to get your solution folder at design time. So, technically, I am not answering your question, just offering a potential workaround.
You can check if your control is in DesignMode
and if it is, you can use Assembly.GetExecutingAssembly()
to get the Assembly
for your control and determine the location it was loaded from.
Note that there are some caveats with the DesignTime
property value, namely if your designing your control or if you are designing a Form that contains your control, it'll return properly true, but if you are designing a form that contains a control that contains your control, it'll retun false.
You might want to skip the whole DesignTime check because of this and always look for the NHibernate config in the base path of your assembly, if your standard way to find that config file fails.
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);
Of course I used VisualStudio.DTE.10.0, you should probably use VisualStudio.DTE.9.0.
Good luck!
I'm way late, and I have no idea if this works in VS2008, but for VS2015, the simplest thing I could think to do was create a t4 template. Here's the contents:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".generated.cs" #>
namespace MyNamespace
{
public static class BuildVariables
{
public static readonly string SolutionDir = <#= QuotedString(Host.ResolveAssemblyReference(@"$(SolutionDir)")) #>;
}
}
<#+
public string QuotedString(string value)
{
if (value == null)
{
return "null";
}
return "@\"" + value.Replace("\"", "\"\"") + "\"";
}
#>
This generates a class that's has exactly what's needed. The two warnings I have for this are to be sure to ignore it from version control, and watch out if you copy your solution somewhere else. Unfortunately, building t4 templates as part of an automated build process is still an complex problem in 2018.
I ended up embedding the config file. Since the program auto updates independently of the config file I could give up exposing it to the users.