开发者

Placing C# Project References in a Subfolder

I have a solution with multiple projects that all output dlls (except for the main application of course). Copy local is set to true for all of the references and everything is fine and dandy with dlls in the same directory as the exe.

My problem is that this is ugly. I'd like to put all the dll's in a subfolder (actually two subfolders down to be precise). How can I do this in Visual Studio 2008?

I've found a few开发者_JS百科 questions that seem similar but I couldn't find the simple answer that I know has to exist.

EDIT: To be clearer, I want to know how to make the assembly loader look for references somewhere besides the operating directory. Users will be interacting with some of the other files in the directory, and the less clutter there is for them the better.

EDIT 2: I also want to avoid using the GAC. The application needs to be self contained.


Have you tried the AppDomain namespace?

AppDomain.CurrentDomain.AppendPrivatePath

http://www.vcskicks.com/csharp_assembly.php


Or AssemblyResolve

public static class AssemblyResolver { 
    static AssemblyResolver() { 
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(delegate(object sender,  ResolveEventArgs args) {
            return Assembly.LoadFrom(...); 
        }); 
    }  
} 


Use the app.config <probing>element to instruct the .NET Runtime to look in the subfolders to locate additional assemblies. See here.


You can't put those references in a subfolder. Since they will not be "seen" by your application's run-time.

The first place to put them is in your debug directory then in the Global Assembly Cache (aka GAC). Note that what you see in the (.Net) tab in Add Reference dialog is actually the references in the GAC directory.

Note: If you use TFS as a backend source control, take notice that reference are not copied to the source control repository when you perform a check-in, rather you have to copy them manually.


I just publish an article that explain all these with details. Partitioning Your Code Base Through .NET Assemblies and Visual Studio Project

Here are the resulting guidelines of the article:

  • Reduce drastically the number of assemblies of your code base.
  • Create a new assembly only when this is justified by a specific requirement for physical separation.
  • In a Visual Studio project, use ‘reference by assembly’ instead of ‘reference by Visual Studio project’.
  • Never use the Visual Studio referencing option ‘Copy Local = True’.
  • Put all VS solutions and Build action .bat files in a $rootDir$ directory.
  • Compile all assemblies in directories: $rootDir$\bin\Debug and $rootDir$\bin\Release
  • Use the directory $rootDir$\bin to host tests assemblies.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜