查找以编程方式给定的.exe文件的路径 [英] find path programetically given .exe file

查看:84
本文介绍了查找以编程方式给定的.exe文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抚慰大家,
以编程方式找到给定的.exe文件的路径
如果回复非常合适.
谢谢U.

Hellow every body,
find path programetically given .exe file
if reply greatly appriciate.
Thank U.

推荐答案

问题的措辞很差,所以没有人可以100%确定您的意思.

对于当前运行的应用程序,您可以找到其入口程序集主模块的路径,该路径通常是一些* .EXE文件:

The question is poorly formulated, so no one can be 100% sure what path do you mean.

For a currently running application, you can find a path to the main module of its entry assembly, which is usually some *.EXE file:

string exeFile =
   System.Reflection.Assembly.GetEntryAssembly().Location;



注意:还有其他几种方法,但是其中一些方法不可靠(不够通用),因为结果取决于应用程序的托管方式;例如,当由服务控制器甚至由Visual Studio调试主机作为Windows Service应用程序承载时,它可能会给出不同的结果.

—SA



Note: there are several other methods, but some of them are not reliable (not universal enough) as the result depends on how the application is hosted; for example, it might give different result when hosted as a Windows Service application by a Service Controller or even by Visual Studio debug host.

—SA


大家好,我终于在我的帮助下解决了这个问题.
//解决方案
//这样的方法
静态IEnumerable< string>搜索(字符串根,字符串searchPattern)
{
队列< string> dirs = new Queue< string>();
dirs.Enqueue(root);
while(dirs.Count> 0)
{
字符串dir = dirs.Dequeue();
//文件
string []路径= null;
试试
{
路径= Directory.GetFiles(dir,searchPattern);
}
赶上
{
}
//吞下
if(paths!= null&& paths.Length> 0)
{
foreach(路径中的字符串文件)
{
收益申报文件;
}
}
//子目录
路径= null;
试试
{
路径= Directory.GetDirectories(dir);
}
赶上
{
}
//吞下
if(paths!= null&& paths.Length> 0)
{
foreach(路径中的字符串subDir)
{
dirs.Enqueue(subDir);
}
}
}
}


//像这样调用tat方法
foreach(搜索"C:\\","AcroRd32.exe"中的字符串匹配))
{
Response.Write(match);
返回;
}
Hi guys finally i have solved this answer with help of my tl.
//solution
//method like this
static IEnumerable<string> Search(string root, string searchPattern)
{
Queue<string> dirs = new Queue<string>();
dirs.Enqueue(root);
while (dirs.Count > 0)
{
string dir = dirs.Dequeue();
// files
string[] paths = null;
try
{
paths = Directory.GetFiles(dir, searchPattern);
}
catch
{
}
// swallow
if (paths != null && paths.Length > 0)
{
foreach (string file in paths)
{
yield return file;
}
}
// sub-directories
paths = null;
try
{
paths = Directory.GetDirectories(dir);
}
catch
{
}
// swallow
if (paths != null && paths.Length > 0)
{
foreach (string subDir in paths)
{
dirs.Enqueue(subDir);
}
}
}
}


//call tat method like this
foreach (string match in Search"C:\\","AcroRd32.exe"))
{
Response.Write(match);
return;
}


这篇关于查找以编程方式给定的.exe文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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