简体   繁体   English

访问项目应用程序文件夹中的程序集dll时出现FileLoadException

[英]FileLoadException while accessing assembly dlls in project application folder

I want to load assemblies stored inside my project application folder on the c: drive. 我想加载存储在c:驱动器上的项目应用程序文件夹中的程序集。

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. 无法加载文件或程序集'file:/// C:\\ SampleProj \\ Workspace \\ Test_App \\ bin \\ Debug \\ Assemblies'或其依赖项之一。 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 单击“解除阻止”按钮获取所有DLL属性

Please help. 请帮忙。

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). 正如我在评论中所写,您未指定有效路径(当您需要指定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)));   
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM