如何在C#中获取64位进程的可执行路径 [英] How to get a 64-bit process's executable path in C#

查看:330
本文介绍了如何在C#中获取64位进程的可执行路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从64位应用程序获取进程的可执行路径.使用以下代码仅适用于32位应用程序,但会在64位应用程序上引发错误.

 Process proc = Process.GetProcessById(( int )pid);
返回 proc.MainModule.FileName; 

是否有办法在64位应用程序上获取可执行文件的完整路径? >解决方案

感谢您回答我的问题和澄清.我确认这个问题.好吧,这似乎确实可以这种方式起作用.在我看来,WoW64似乎有些特殊之处(32位应用程序可在Windows上用于64位指令集体系结构 ^ ]).

我为什么这么认为呢?因为我也测试了相反的情况:64位.NET应用程序在32位进程上获取进程信息,而不是.NET.在这两种情况下,以管理员身份运行都没有任何区别,这实际上是指令集体系结构问题.

这是一个实际的结论:在这样的应用程序中,始终使用平台目标任何CPU",这将使您的代码在所有9种情况下都能正常工作(三种不同的指令集体系结构:x86,x86-64,IA-64(Itanium ),所有组合).

—SA


尝试此

 公共 字符串 GetExeFFN()
{
    组装a = System.Reflection.Assembly.GetExecutingAssembly();
    字符串 mExeFFN = a.CodeBase.Substring( 8 );
    mExeFFN = mExeFFN.Replace(' /''  \\');
    返回 mExeFFN;
} 


Get a process''s executable path from a 64-bit application. Using the following code only works for 32-bit applications but throws an error on 64-bit apps.

Process proc = Process.GetProcessById((int)pid);
return proc.MainModule.FileName;

Is there a way to get the full path to the executable file on 64-bit applications?

解决方案

Thank you for answering my question and clarification. I confirm the problem. Well, it appears to be something that really works this way. To me, it looks like some peculiarity in WoW64 (something a 32-bit application works with on Windows for 64-bit instruction-set architectures, http://en.wikipedia.org/wiki/WOW64[^]).

Why do I think so? Because I also tested the opposite case: when a 64-bit .NET application fetch process information on a 32-bit process, not a .NET one. In both cases, running as administrator did not make any difference, this is really an instruction-set architecture problem.

Here is one practical conclusion: in such application, always use the platform target "Any CPU", it will make you code working correctly in all 9 cases (three different instruction-set architectures: x86, x86-64, IA-64 (Itanium), in all combinations).

—SA


try this

public String GetExeFFN()
{
    Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
    String mExeFFN = a.CodeBase.Substring(8);
    mExeFFN = mExeFFN.Replace('/', '\\');
    return mExeFFN;
}


这篇关于如何在C#中获取64位进程的可执行路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆