Confirm strong name of current assembly

Is it possible to check the strong name of a .NET application that is already running separately from your own application execution 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 a way to do this without using reflection?

+3
source share
5 answers

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);
    }
+2
source

Without reflection:

, . , PE, .

+4

:

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; }
}
+2

, , - AssemblyName.

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

Process.Modules dll exes, . , , . ( name).

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

. ,

0

" " exe:

using System.Diagnostics;

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

}
-1

Source: https://habr.com/ru/post/1734378/


All Articles