views:

323

answers:

5

Is it possible for one to check the strong name of a .NET application that is already currently running separately from your own running applications process?


EDIT: For clarification, a solution that does not require a hard coded path to the executing assembly would be the most ideal solution.


EDIT #2: Is there any way to do this without using reflection?

A: 

If by "strong name" you mean the exe name:

using System.Diagnostics;

if (Process.GetProcessesByName("whatever.exe").Length > 0)
{
     //do something

}
sam munkes
No, I mean the strong name.
Kyle Rozendo
strong name as in application title?
sam munkes
Strong naming as in http://msdn.microsoft.com/en-us/library/wd40t7ad(VS.71).aspx
Kyle Rozendo
+2  A: 

Does this give you what you are looking for?

    Process[] processlist = Process.GetProcesses();

    foreach(Process theprocess in processlist)
    {
        string strongName = "N/A";
        try
        {
            strongName = Assembly.ReflectionOnlyLoadFrom(theprocess.MainModule.FileName).FullName;
        }
        catch
        {
            // System process?
        }
        Console.WriteLine("Process: {0} ID: {1} Strong Name: {2}", theprocess.ProcessName, theprocess.Id, strongName);
    }
37Stars
Do you know of a way to do this without reflection?
Kyle Rozendo
I know of no other way to do this.
37Stars
+1 and accepted as I used this as a solution for the time being. Thanks.
Kyle Rozendo
+2  A: 

That should work:

public static bool IsStrongNamed(string assemblyPath)
{
    try
    {
        Assembly a = Assembly.ReflectionOnlyLoadFrom(assemblyPath);
        byte[] publicKey = a.GetName().GetPublicKey();

        return publicKey.Length > 0;
    }
    catch { return false; }
}

public static bool GetStrongName(string assemblyPath)
{
    try
    {
        Assembly a = Assembly.ReflectionOnlyLoadFrom(assemblyPath);

        return a.FullName;
    }
    catch { return string.Empty; }
}
SelflessCoder
Do you know of a way to do this without reflection?
Kyle Rozendo
You mean without ReflectionOnlyLoadFrom? Why don't you want to use that? It allow you to inspect an assembly without really loading it in your process.
SelflessCoder
It still unfortunately locks the file of the process.
Kyle Rozendo
Then you could use andras solution with PE headers, but in that case it might be easier to just copy the dll in a local folder and then use ReflectionOnlyLoad.
SelflessCoder
+4  A: 

Without reflection:

If you know the process, you know the filename. If you know the filename, you can process the PE headers to find the strong name signature.

andras
For getting the file name from the process handle: http://stackoverflow.com/questions/1219951/win32api-how-to-get-file-name-of-process-from-process-handle
andras
+1 - I didn't accept this as I will only be able to use it at a later stage (if at all), but thank you for the insight.
Kyle Rozendo
@Kyle Rozendo: you're welcome.
andras
A: 

Hmm I think the solution to Your problem is the AssemblyName class.
First

Process.GetProcesses().Where(p => p.ProcessName = nameUWant); //maybe single or default?

then with each process take Process.Modules to get dlls or exes loaded by this process. After You obtain the name You want. (the module has name property). Then use

AssemblyName.GetAssemblyName().GetPublicKeyToken() != null

This should work. hope it helps

luckyluke