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.
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.
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.
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;
}
}
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());
}
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.