I wrote a small application to get the version numbers of the files contained in a .cab file.
I extract all the files from the cab to the temp directory and look at all the files and extract the 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 done, I want to delete all the files that were 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 when .net.exe sometimes crashes. I can manually delete the file so that it is not locked.
What should I look for to make this work? Can Assembly.LoadFile lock a file?
source share