C# Programming > Miscellaneous

C# Reference Assembly
Custom Path

C#.NET Assembly

A C# assembly is basically any C# library. This means many C#.NET application commonly use reference assemblies in order to execute specialized parts of source code.

For example, let's say you have a C#.NET application that processes images. This application uses all kinds of filters to modify images in different ways. Each filter's source code is in a C# library (dll) so it will be easier to update and maintain.

Now the main C# application will reference the libraries (reference assemblies) for the needed source code when a user selects a certain filter.

Using assemblies and libraries in C#.NET can be essential in creating a well structured application that is easy to maintain and expand.

Default Reference Path

The default path that a C#.NET application will look for a reference assembly, is directly in the same path. This means if you run the main C# program (let's say MainApp.exe) from the path C:\MyFolder\MainApp.exe. The program will expect all needed assemblies (our C# libraries) to be in the folder C:\MyFolder.

Setting a Custom Reference Path

Let's go back to our example image processing C# application. As the amount of filters increasing, having everything within one folder is both messy and unprofessional The solution is to place all common C# libraries, in this case all the filter dll's, in a subfolder of where the main C#.Net application is located.

So let's say you want the main application in C:\MyFolder\MainApp.exe and the reference assemblies in C:\MyFolder\Filters.

Here is how to tell the C# program to look for assemblies within its startup path and in a custom reference path. Go to where the Main void is (commonly in Program.cs). Add the following line before the main Form is initialized:

AppDomain.CurrentDomain.AppendPrivatePath("Filters");

Usually the line that would follow would be:

Application.Run(new Form1());

Notice that all you needed to add was the name of the subfolder and not the entire path. This is so you could move your C# application to a new folder and the C# code would still search for the same subfolders.

Also notice that the function appends a private path. Meaning you can add as many subfolders as you wish as reference paths.

Back to C# Article List