Search code examples
c#.net.net-assembly

Assembly.LoadFile() and crash


I use Assembly.LoadFile(string path) to load assembly to C# program. It works perfectly on my PC and two notebooks but... when I tried to send this app to my friend it crashed just after this call without any exceptions. We use same versions of .NET Framework, everything must be fine. I cant understand what happens. No exceptions, no errors, just "silent" return. I also tried to use LoadFrom but nothing changed. I use absolute path for dll files

public LoadedType[] LoadFrom(string path)
        {
            Assembly assembly = Assembly.LoadFile(path);
        }

and calling method is

Loader loader = new Loader();
        string[] paths = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.dll", SearchOption.TopDirectoryOnly);
        List<string> corrupted = new List<string>();

        foreach (string path in paths)
        {
            try
            {
                LoadedType[] loadedTypes = loader.LoadFrom(path);
                MessageBox.Show("loaded");

                if (loadedTypes.Length == 0)
                {
                    continue;
                }
                foreach (LoadedType loadedT in loadedTypes)
                {
                    AvailableTypes.Add(loadedT);
                }
            }
            catch (ReflectionTypeLoadException)
            {
                corrupted.Add(Path.GetFileName(path));
            }
        }

MessageBox does not appear. Could somebody explain me whats wrong and why this code works on three PCs and does not work on another two PCs with the same Framework version?


Solution

  • using the code above you can not know if is there an exception or not, because you catch just exception of type ReflectionTypeLoadException, add another catch(Exception ex).

    Check that string[] paths is not empty.

    check that these assemblies are not used by another process.

    check that you have access to read these assemblies.