我编写了一个小应用程序来获取 .cab 文件中包含的文件的版本号。
我将 cab 中的所有文件提取到一个临时目录并遍历所有文件并检索版本号,如下所示:
//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);
然后完成后我想删除所有已提取的文件,如下所示:
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);
}
这适用于所有文件,除了有时会在 .net .exe 上失败。我可以手动删除文件,这样它就不会被锁定。
我应该寻找什么来完成这项工作?Assembly.LoadFile 是否可能锁定文件?