0
Reply

AssemblyName.GetAssemblyName - BadImageFormatException not Working

Gavin Roberts

Gavin Roberts

Nov 30 2005 7:38 AM
4.9k

Hey peeps,

I've created an Self Update class for my applications, and it's working fine for all my applications apart from one. What it does first is generates a manifest of all the files found in the startup path of the executable. So it uses DirectoryInfo to go through each file and get the following information.

Filename, path to file, date created, size, date last writen to and version. Now to get the version, I am using AssemblyName.GetAssemblyName(Full Path to File).Version.ToString() which if the object has an assembly manifest, it will return the version information for that file. If the file does not have an assembly manifest, it will throw an BadImageFormatException, in which I then just tell it to return the value to "".

But for some weird reason, it is throwing the exception as it should but not executing the catch statement, instead it throws an IDE internal exception window and highlights the line in question in green.

I rebuilt the dll and copied it into another applications directory that uses the update feature and that works fine. No errors at all.

I've now tried moving the code that does that to a new method, and it still doesn't work. ( Just in case vs.net 2003 has cached the original )

If anyone can help at all, i'd really appreciate it.

TIA

Gavin

Files


BackgroundCopyManager.dll
ICSharpCode.SharpZipLib.dll
invClasses.dll
invoiceGenerator.dll
InvoiceWizard.exe
InvoiceWizard.exe.config
InvoiceWizard.pdb
Invoice_WizardClass.dll
Invoice_WizardClass.pdb
itextsharp.dll
Microsoft.Msdn.Samples.BITS.dll
SIDUtilities.dll
sUpdate.dll
sUpdate.pdb

Coding

private void GenerateLocalManifest()
  {
   try
   {
    XmlDocument Base = new XmlDocument();
    XmlElement Root = Base.CreateElement("Manifest");
    XmlAttribute Root_Name = Base.CreateAttribute("ApplicationName");
    Root_Name.Value = getApplicationName();
    Root.Attributes.Append(Root_Name);

    DirectoryInfo BaseFileSystem = new DirectoryInfo(Application.StartupPath);
    foreach(FileInfo BaseFile in BaseFileSystem.GetFiles())
    {
     if(BaseFile.Name != "sUpdate.dll" && BaseFile.Name != getApplicationName() + ".exe")
     {
      XmlElement RootFile = Base.CreateElement("File");

      XmlAttribute Filename = Base.CreateAttribute("Name");
      Filename.Value = BaseFile.Name;
      RootFile.Attributes.Append(Filename);

      XmlAttribute Filepath = Base.CreateAttribute("Path");
      Filepath.Value = BaseFile.FullName;
      RootFile.Attributes.Append(Filepath);

      XmlAttribute Filesize = Base.CreateAttribute("Size");
      Filesize.Value = BaseFile.Length.ToString();
      RootFile.Attributes.Append(Filesize);

      XmlAttribute FileCreated = Base.CreateAttribute("Created");
      FileCreated.Value = BaseFile.CreationTime.ToString("s");
      RootFile.Attributes.Append(FileCreated);

      XmlAttribute FileModified = Base.CreateAttribute("Modified");
      FileModified.Value = BaseFile.LastWriteTime.ToString("s");
      RootFile.Attributes.Append(FileModified);

      XmlAttribute FileVersion = Base.CreateAttribute("Version");
      FileVersion.Value = getFileVersion(BaseFile);
      RootFile.Attributes.Append(FileVersion);

      Root.AppendChild(RootFile);
     }
    }
    Base.AppendChild(Root);

    Local_Manifest = Base.OuterXml;
   }
   catch(Exception e)
   {
    Updater_Connection.LogError(getApplicationName(), e.Message, MAC , true);
    UpdateErrorArgs Args = new UpdateErrorArgs(e, null);
    Update_Error(Args);
   }
  }

  private string getFileVersion(FileInfo Filename)
  {
   string ReturnValue = "";
   try
   {
    ReturnValue = AssemblyName.GetAssemblyName(Filename.FullName).Version.ToString();
   }
   catch(System.BadImageFormatException)
   {
    ReturnValue = "";
   }
   return ReturnValue;
  }