Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
I am using reflection to find inner content of assembly. Now i want to know whether the specified dll with specified version is present in a perticular path or not. please give code in c#.net
Posted

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:
C#
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 { //exclusion from the common rule of exceptions use
        return VersionCheckResult.NotAValidAssembly;
    } //exception
}


[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
 
Share this answer
 
v2
Comments
Tarun.K.S 7-Jul-11 7:04am    
5+! :)
Sergey Alexandrovich Kryukov 7-Jul-11 12:27pm    
Thank you very much, Tarun.
--SA
 
Share this answer
 
Comments
Member 7962316 4-Jul-11 5:46am    
suppose iam inserting a new assembly dynamically into GAC, then dynamically i want to know whether the inserted asembly with specified version is present in GAC or not. please give me code in c#.net
Have a look at this article. http://vbcity.com/blogs/jatkinson/archive/2010/01/03/reading-the-global-assembly-cache-gac-in-vb-net.aspx[^]

It will give you the list of all Assemblies along with it's version. Then you can compare these versions with the one that you extracted using Reflection.

Hope it helped. Good luck. :thumbsup:

UPDATE:
Here is the C# code:

C#
string utilPath = @"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\gacutil.exe";

            Process gacProcess = new Process();
            gacProcess.StartInfo.FileName = utilPath;
            gacProcess.StartInfo.CreateNoWindow = true;
            gacProcess.StartInfo.RedirectStandardOutput = true;
            gacProcess.StartInfo.UseShellExecute = false;
            gacProcess.StartInfo.Arguments = "/l";

            gacProcess.Start();

            StreamReader gacOutput = gacProcess.StandardOutput;

            while (!gacOutput.EndOfStream)
            {
                string output = gacOutput.ReadLine();
                try
                {
                    AssemblyName asm = new AssemblyName(output);
                    string version = asm.Version.ToString();  //Here you will get the version of the assembly
                }
                catch (Exception ex)  //Catch the exception but do nothing
                { }                
            }

            gacOutput.Close();

Also you can use this tool: http://www.developerfusion.com/tools/convert/vb-to-csharp/[^] to convert vb.net to c#
 
Share this answer
 
v3
Comments
Member 7962316 4-Jul-11 6:44am    
I think i will work out. but i need that code in c#.net. so please give me that code in c#.net...
Member 7962316 4-Jul-11 7:10am    
ok. then where should i give my assembly name and version to check whether my assembly is already exist or not
Sergey Alexandrovich Kryukov 4-Jul-11 17:01pm    
Tarun, I think OP is asking about particular path, not GAC.

Please see my solution; I think this is what OP needs.
--SA
Tarun.K.S 5-Jul-11 5:09am    
The reason why I specified GAC was because the OP commented on the RaviRanjankr's answer related to GAC. I'm not sure your solution works either, OP has got us confused.
Sergey Alexandrovich Kryukov 5-Jul-11 6:09am    
I figured about GAC and updated my answer, thank you.
I don't understand why you run anything (start process). I think I answer to the point now.
--SA
you can use io.File.exists(Filepath) for determine that file is exist or not and you can easily found out the version with the help of relection
try this link, on here example show to get file version and other things:

http://www.authorcode.com/how-to-get-assambly-version-through-reflection-in-net/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900