以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Dot NET,C#,ASP,VB 』  (http://bbs.xml.org.cn/list.asp?boardid=43)
----  C# Reference Assembly Custom Path  (http://bbs.xml.org.cn/dispbbs.asp?boardid=43&rootid=&id=79655)


--  作者:卷积内核
--  发布时间:1/26/2010 11:42:00 AM

--  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.


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
7,402.344ms