验证正在运行的程序集的强名称

发布于 2024-08-22 11:40:54 字数 159 浏览 9 评论 0原文

是否可以检查当前已经与您自己正在运行的应用程序进程分开运行的 .NET 应用程序的强名称?


编辑:为了澄清,不需要执行程序集的硬编码路径的解决方案将是最理想的解决方案。


编辑#2:有没有办法使用反射来做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

随心而道 2024-08-29 11:40:54

没有反思:

如果你知道这个过程,你就知道文件名。如果您知道文件名,则可以处理PE标头以查找强名称签名

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.

空‖城人不在 2024-08-29 11:40:54

这能为您提供您正在寻找的东西吗?

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

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);
    }
倾`听者〃 2024-08-29 11:40:54

那应该有效:

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

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; }
}
夜夜流光相皎洁 2024-08-29 11:40:54

嗯,我认为解决您问题的方法是 AssemblyName 类。
首先

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

,每个进程都使用 Process.Modules 来获取该进程加载的 dll 或 exe 文件。
当您获得您想要的名称后。 (该模块具有 name 属性)。
然后使用

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

这应该可以工作。
希望有帮助

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

邮友 2024-08-29 11:40:54

如果“强名称”是指 exe 名称:

using System.Diagnostics;

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

}

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

using System.Diagnostics;

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文