1

Possible Duplicate:
How do I tell if a win32 application uses the .NET runtime

I have a file name, and I check this file exist in current directory, my problem is I want to know this file( the file is executable) is .Net exe or regular exe.

Community
  • 1
  • 1
MRM
  • 435
  • 2
  • 10
  • Is this a programming question, or are you simply asking how to find out? – Oded Feb 22 '12 at 14:08
  • related: http://stackoverflow.com/questions/2638883/how-does-windows-differentiate-between-a-regular-exe-and-a-net-exe – AakashM Feb 22 '12 at 14:09
  • Difficult because the OS doesn't know. The .exe contains code that launches the CLR and passes the assembly to the CLR for execution. You could try finding some signature of that code and looking for it in the file, or searching the file for signatures related to the metadata. – James Gaunt Feb 22 '12 at 14:09
  • You want to serach for EXE files? – Simon Edström Feb 22 '12 at 14:09
  • in my problem, depend on the kind of these exe I must do some routin – MRM Feb 22 '12 at 14:09
  • @MRM - A programming question requires code. I remove the VB.NET and C# since this question is about the .NET Framework. – Security Hound Feb 22 '12 at 14:09
  • For a programmatic approach; read the .exe header info; more info on http://support.microsoft.com/kb/65122 – Marvin Smit Feb 22 '12 at 14:16
  • every one, thanks for your helps. – MRM Feb 22 '12 at 14:26

4 Answers4

1

Construct an AssemblyName, and call Assembly.Load in a try-catch block. If you do not get an exception, it's a .NET assembly. This is not bullet-proof, but may provide a reasonable way to get started.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You could use the Assembly.LoadFrom method. This will throw an exception if the assembly is not a .NET assembly, or relies on a newer version of the framework. As long as you are using the latest version of the framework, the following sample will only return false when the path is not a .NET assembly.

public bool IsNetAssembly (string path)
{
    try
    {
         Assembly assembly = Assembly.LoadFrom(path);
         return true;
    }
    catch (BadImageFormatException e)
    {
         Console.WriteLine("This is either not a .NET assembly, or is " + 
                           "later than the current .NET framework");
         return false;
    }
}
RB.
  • 36,301
  • 12
  • 91
  • 131
1

You can do it like this:

try
{
    MessageBox.Show(System.Reflection.Assembly.LoadFrom(@"C:\test\csharptestapp.exe").ImageRuntimeVersion);         //Valid, will return 4.0
    MessageBox.Show(System.Reflection.Assembly.LoadFrom(@"C:\Windows\notepad.exe").ImageRuntimeVersion);            //Invalid, will throw BadImageFormatException
}
catch (BadImageFormatException ex)
{
    MessageBox.Show(ex.ToString());
}
John Koerner
  • 37,428
  • 8
  • 84
  • 134
0

There's a similar question here: How do I tell if a win32 application uses the .NET runtime

It says to use PEVerify Tool from Microsoft.

Community
  • 1
  • 1
jon
  • 5,986
  • 5
  • 28
  • 35