I’ve written a small app to get version numbers of files contained within a .cab file.
I extract all files from the cab to a temp directory and loop through all the files and retrieve version numbers as follows:
//Attempt to load .net assembly to retrieve information
Assembly assembly = Assembly.LoadFile(tempDir + @"\" + renameTo);
Version version = assembly.GetName().Version;
DataRow dr = dt.NewRow();
dr["Product"] = renameTo;
dr["Version"] = version.ToString();
dt.Rows.Add(dr);
Then when finished I want to delete all the files that have been extracted as follows:
foreach (string filePath in filePaths)
{
//Remove read-only attribute if set
FileAttributes attributes = File.GetAttributes(filePath);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
File.SetAttributes(filePath, attributes ^ FileAttributes.ReadOnly);
}
File.Delete(filePath);
}
This works for all files except except sometimes fails on .net .exe’s. I can manually delete the file so it does not appear to be locked.
What should I be looking for to make this work? Is Assembly.LoadFile perhaps locking the file?
AssemblyName.GetAssemblyNamewill get theAssemblyNameobject without locking the file on you. see msdn. You can get the version from there.