Để có được thư mục bin, string path = Assembly.GetExecutingAssembly().Location;
không NOT luôn hoạt động (đặc biệt khi assembly thực thi được đặt trong một thư mục tạm thời ASP.NET).
Thay vào đó, bạn nên sử dụng string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin");
Hơn nữa, có lẽ bạn nên đưa ra fileloadexception và BadImageFormatException vào xem xét.
Đây là chức năng làm việc của tôi:
public static void LoadAllBinDirectoryAssemblies()
{
string binPath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin"); // note: don't use CurrentEntryAssembly or anything like that.
foreach (string dll in Directory.GetFiles(binPath, "*.dll", SearchOption.AllDirectories))
{
try
{
Assembly loadedAssembly = Assembly.LoadFile(dll);
}
catch (FileLoadException loadEx)
{ } // The Assembly has already been loaded.
catch (BadImageFormatException imgEx)
{ } // If a BadImageFormatException exception is thrown, the file is not an assembly.
} // foreach dll
}
Nguồn
2011-04-08 18:56:34
Bạn sẽ có lẽ cũng muốn để thêm một kiểm tra để đảm bảo bạn không thêm Assembly mà bạn đang thực sự đang chạy :) – Wolfwyrd
Biến 'path' chứa tên tập tin thư mục, nó cần phải được rút ngắn bằng' Path.GetDirectoryName (đường dẫn) ' – cjk
đã được cập nhật để phản ánh nhận xét ở trên. –