iamacrazydeveloper
iamacrazydeveloper

Reputation: 1

FileLoadException while accessing assembly dlls in project application folder

I want to load assemblies stored inside my project application folder on the c: drive.

This is the code:

public static void Main(string[] args)
{                    
    Assembly asm = null;
    asm = Assembly.LoadFrom("C:\\SampleProj\\Workspace\\Test_App\\bin\\Debug\\Assemblies");
}

The exception I am getting is:

Could not load file or assembly 'file:///C:\SampleProj\Workspace\Test_App\bin\Debug\Assemblies' or one of its dependencies. Access is denied.

I tried the following, but the error remains the same:

  1. Keep the project in other drive and access assemblies from there
  2. Removed read-only permission from project folder-sub folders
  3. Granted full control permissions to Users in project folder properties
  4. Clicked on Unblock button for all DLL properties

Please help.

Upvotes: 0

Views: 172

Answers (1)

Tzah Mama
Tzah Mama

Reputation: 1567

As I wrote in the comment, you are not specifying a valid path (you are specifying a folder when you need to specify a dll). If you want to load all the assemblies in that folder use this piece of code:

private static List<Assembly> Assemblies = new List<Assembly>();

private static void LoadAllAssemblies(string path)
{
    foreach (var dir in Directory.GetDirectories(path))
    {
        LoadAllAssemblies(Path.Combine(path, dir));
    }

    foreach (var file in Directory.GetFiles(path))
    {
        if (Path.GetExtension(file) == ".dll")
        {
             Assemblies.Add(Assembly.LoadFrom(Path.Combine(path, file)));   
        }
    }
}

Upvotes: 1

Related Questions