I assume "specified DLL… in particular path" means you know exact full path name of the assembly's main executable module (usually, this is just one DLL file as Visual Studio supports only single-module assemblies).
So, the problem is reduced to the comparison of the version with some required version after you check that the file exists. (If you don't know exact file name you should simply apply the same method to all files in the directory.)
Here is how:
enum VersionCheckResult {
FileNotFound, NotAValidAssembly,
VersionEarlierThenRequired, VersionLaterThenRequired,
VersionMatch, }
VersionCheckResult CheckAssemblyFile(
string fileName,
System.Version requiredVersion) {
if (!System.IO.File.Exists(fileName))
return VersionCheckResult.FileNotFound;
try {
System.Reflection.Assembly assembly=
System.Reflection.Assembly.LoadFrom(fileName);
System.Version version = assembly.GetName().Version;
if (version < requiredVersion)
return VersionCheckResult.VersionEarlierThenRequired;
else if (version > requiredVersion)
return VersionCheckResult.VersionLaterThenRequired;
else
return VersionCheckResult.VersionMatch;
} catch {
return VersionCheckResult.NotAValidAssembly;
}
}
[EDIT]
If I did not read your question correctly; you might mean by Name not the path name but assemblies strong name. In this case, you should compare the name, not version (or not just version). In this case
Assembly.GetName
and
Assembly.Location
— it will give you the exact full path of the assembly's main executable module; you can extract the directory name using
System.IO.Path.GetDirectoryName
. For example, if your assembly is in CAG, you can load the assembly using its name rather then path using
Assembly.Load(AssemblyName)
. When it is successfully done, you can find out it's path name using
<code>
Assembly.Location.
In this way, you can do a round trip: starting from the path name find string assembly name and visa versa.
Please see
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[
^] for more detail.
—SA