How do you get the solution directory in C# (VS 2008) in code?

后端 未结 6 2362
时光取名叫无心
时光取名叫无心 2020-12-10 05:29

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

6条回答
  •  误落风尘
    2020-12-10 06:11

    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.

提交回复
热议问题