none
How to read the file attributes of a non .NET dll? RRS feed

  • Question

  • Hi.

    I wrote a small C#.net 4 app that displays a number of different attributes for .dll's and .exe's.  Only, for some non .NET .dll's I'm getting errors.  The line of code that seems to offend is:

                        Assembly asm = Assembly.LoadFrom(txtAssemblyName.Text);

    After doing some reading I think the problem is that when my app tries to read a .dll via the Assembly.LoadFrom() method that was not created using .NET, chaos and errors ensue such as:

          "The module was expected to contain an assembly manifest"

          "An attempt was made to load a program with an incorrect format"

    I did some looking and I cannot seem to find which classes I should be using to get to a non .NET file's attributes.  Can someone help me out here?  If I can't use the Assembly class to get a file's attributes, what class should I use?

    Any help you can provide would be most appreciated.

    Thanks.

    Logan


    Thursday, March 14, 2013 5:32 PM

Answers

  • I suggest you to use a try/catch statement and (if Exception is not thrown) you can access to all information because you have a .Net Assembly.

    In case of Exception you can call another function dedicated to "C# get COM DLL infos" and you can try this (I do not know if it works for .dll), it is the first link I found: System.Diagnostic.FileVersionInfo.

    Note that a .Net library for .Net 4 do not work on a O.S. that haven't that version of .Net installed (or higher). Often a .Net software installation contains the necessary .Net Framework embedded (the "client" version) or a link to the corresponding download/installation.
    (Sorry, I'm not very precise on this, but the concept is right). You have to consider this.

    Thursday, March 14, 2013 7:49 PM
  • Ok so a suggested by Alessandro you could just use try/catch to avoid an error when the user selects a wrong files...

    I'm not sure if it could fit in your process but you could also use System.Reflection from within your app to get the list of assemblies (possibly such as a swithc in your debug build if this tool is used by some person in your organizatino to manage versioning)...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, March 15, 2013 10:18 AM

All replies

  • You can't load (instantiate) an Assembly (.Net Assembly) from a non .Net library/executable.
    It is like you want to create a .Net class from Java (or other language) compiled code.
    You are mixing apples and pears.

    You say: for some non .NET .dll's I'm getting errors
    I think you had errors for ALL non .Net DLL. Is it right?

    Obtaining information of files is a completely different matter: see FileInfo class.
    I hope it is what you are searching for.

    Thursday, March 14, 2013 6:11 PM
  • Hi,

    "Attributes" is quite vague (do you mean some file metadata ? you also have "attributes" such as readonly that could be set on files etc...) and it doesn't seems to me Assembly gives anything you would really want even if it were really a .NET file. If this is for images you do have http://msdn.microsoft.com/en-us/library/system.drawing.image.getpropertyitem.aspx

    If general files you'll likely have to use the Windows API (try to search for an IpPropertyStorage library)

    Or we could discover that you try to read something that just doesn't have what you search (for example an "exported" DLL doesn't have a full description of its function, you always have to provide this description yourself using the DllImport "attribute").

    Please try to be explicit about the info you are trying to get...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Thursday, March 14, 2013 6:17 PM
  • Thanks to both replies.

    I am trying to get a file's attributes, such as assembly file version, assembly version, copyright, etc.  Though I have not verified if it's all non .NET dll's, based on what I read I suspect Alessandro that you are correct.

    I use FileVersionInfo and this gives most of the data I'm looking for, except the assembly version.  For that I was using the Assembly class.

    If there's another way to get the Assembly version info, please let me know.  I do not see how to get this info using the FileInfo or FileVersionInfo classes.

    Thanks again for the very quick reply.

    Logan

    Thursday, March 14, 2013 7:06 PM
  • Assemblies are particular to .NET. You won't find an assembly version on old DLLs files as they are not assemblies (and in particular they don't use the szme versioning mechanism) so AFAIK you won't find anything besides the file and product version shown by FileVersionInfo...

    Not sure what you would have done with this version ? (for example COM DLLs compatibility is handled with unique identifiers known as GUIDs).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


    Thursday, March 14, 2013 7:25 PM
  • We have many, many products in our shop, and each product has many .dll's.  We are in the beginning stages of versioning our assemblies.  Initially we are just updating the file version information and when this is done this small app I'm writing will display both the file version and the assembly version for a quick comparison; to make sure that for now, only the file version was updated and not the assembly version.

    Sometimes the users using this little application cannot tell by name which .dll's are generated by our code and which .dll's are 3rd party .dll's.  So when a user accidentally chooses a .dll not generated by our developers an error is thrown.

    So really, this app is for a quick and easy verification of assembly version vs. assembly file version checks.

    Thursday, March 14, 2013 7:33 PM
  • I suggest you to use a try/catch statement and (if Exception is not thrown) you can access to all information because you have a .Net Assembly.

    In case of Exception you can call another function dedicated to "C# get COM DLL infos" and you can try this (I do not know if it works for .dll), it is the first link I found: System.Diagnostic.FileVersionInfo.

    Note that a .Net library for .Net 4 do not work on a O.S. that haven't that version of .Net installed (or higher). Often a .Net software installation contains the necessary .Net Framework embedded (the "client" version) or a link to the corresponding download/installation.
    (Sorry, I'm not very precise on this, but the concept is right). You have to consider this.

    Thursday, March 14, 2013 7:49 PM
  • Ok so a suggested by Alessandro you could just use try/catch to avoid an error when the user selects a wrong files...

    I'm not sure if it could fit in your process but you could also use System.Reflection from within your app to get the list of assemblies (possibly such as a swithc in your debug build if this tool is used by some person in your organizatino to manage versioning)...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Friday, March 15, 2013 10:18 AM
  • Thanks Alessandro and Patrice for the very quick and precise posts.  Your answers were exactly what I was looking for.

    The System.Diagnostics.FileVersionInfo.GetVersionInfo(txtAssemblyName.Text); worked as I needed it to, where the Assembly.LoadFrom() method threw an exception

    Logan



    Friday, March 15, 2013 12:39 PM