如何知道c#中某个文件将触发哪个进程

发布于 2024-11-16 11:22:45 字数 115 浏览 2 评论 0 原文

我想知道在文件启动之前将触发哪个进程:

Process.Start("PathToFile");

然后我想知道该进程的路径。

谢谢。

I would like to know which process will be fired before a file is started with:

Process.Start("PathToFile");

Then I would like to have the path to the process.

Thank you.

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

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

发布评论

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

评论(3

一向肩并 2024-11-23 11:22:45

您可以查看从 返回的进程的 MainModule 属性Process.Start

Process p = Process.Start(@"D:\\test.txt");
string executableStarted = p.MainModule.FileName; // full path to notepad.exe

但是,您应该记住,Proces.Start 的返回值可能为 null - 根据 MSDN,返回值为:

一个新的 Process 组件
与进程资源相关联,
或 null,如果没有进程资源
已开始(例如,如果现有
进程被重用)。

更新

为了在启动进程之前了解可执行文件,您必须在注册表中的 HKEY_CLASSES_ROOT 下查看。这将是从文件名转到 shell 在打开文件时将执行的命令的代码:

string extension = Path.GetExtension(path);
var regClasses = Microsoft.Win32.Registry.ClassesRoot;
var extensionKey = regClasses.OpenSubKey(extension);
var typeKey = extensionKey.GetValue(String.Empty); 
var cmdKey = regClasses.OpenSubKey(typeKey + @"\shell\open\command");
string command = cmdKey.GetValue(null) as string;

You can look at the MainModule property of the Process returned from Process.Start:

Process p = Process.Start(@"D:\\test.txt");
string executableStarted = p.MainModule.FileName; // full path to notepad.exe

However, you should remember that the return value from Proces.Start might be null - according to MSDN, the return value is:

A new Process component that is
associated with the process resource,
or null, if no process resource is
started (for example, if an existing
process is reused).

Update

In order to know the executable prior to launching the process, you will have to look under HKEY_CLASSES_ROOT in the registry. This would be the code for going from a file name to the command the shell will execute when opening the file:

string extension = Path.GetExtension(path);
var regClasses = Microsoft.Win32.Registry.ClassesRoot;
var extensionKey = regClasses.OpenSubKey(extension);
var typeKey = extensionKey.GetValue(String.Empty); 
var cmdKey = regClasses.OpenSubKey(typeKey + @"\shell\open\command");
string command = cmdKey.GetValue(null) as string;
幽梦紫曦~ 2024-11-23 11:22:45

它返回一个包含更多信息的 Process 对象。 MainModule 可能是适合您的属性。

http://msdn.microsoft.com/en-US/library/system.diagnostics.process.mainmodule(v=VS.80).aspx

编辑:

你想知道之前/之后吗?
开始这个过程? -是的

您可以在注册表中查找注册的文件处理程序 - .doc、.txt 等。

It returns a Process object containing more information. MainModule might be the right property for you.

http://msdn.microsoft.com/en-US/library/system.diagnostics.process.mainmodule(v=VS.80).aspx

EDIT:

Do you want to know it before/without
starting the process? -Yes

You could lookup the registered file handler in the registry - for .doc, .txt, etc.

不一样的天空 2024-11-23 11:22:45

您想要使用Windows文件关联打开的文档

我在这里找到了这个链接,它解释了如何创建文件关联。 这可能会有所帮助。当然,您需要阅读注册表。据我所知有两种格式。

您不知道路径的程序

当未提供路径时,将在当前目录之后查询路径环境变量作为要查找的默认路径。
Path 环境变量可以在这里为您提供帮助。

  public static string GetPath (string pathToFile)
  {
     string fileNameOnly = Path.GetFileName(pathToFile);
     List<string> folders = Environment.GetEnvironmentVariable("Path").Split(';').ToList ();
     folders.Insert(0, Environment.CurrentDirectory);
     foreach (string folder in folders)
     {
        string fileName;
        try
        {
           // Can't trust that the Path environment variable is constructed correctly.
           fileName = Path.Combine(folder, fileNameOnly);
        }
        catch
        {
           continue;
        }
        if (File.Exists(fileName))
           return fileName;
     }
     return null;
  }

编辑:添加了 的链接MS:路径
编辑:添加了另一个链接

Documents that you want to use the windows file association to open

I found this link here that explains how to create a file association. This may help. Of course you'll need to read the registry. There are two formats that I know of.

Programs to which you don't know the path

The path environment variable is consulted after the current directory as default paths to look in when the path is not provided.
The Path environment variable can help you here.

  public static string GetPath (string pathToFile)
  {
     string fileNameOnly = Path.GetFileName(pathToFile);
     List<string> folders = Environment.GetEnvironmentVariable("Path").Split(';').ToList ();
     folders.Insert(0, Environment.CurrentDirectory);
     foreach (string folder in folders)
     {
        string fileName;
        try
        {
           // Can't trust that the Path environment variable is constructed correctly.
           fileName = Path.Combine(folder, fileNameOnly);
        }
        catch
        {
           continue;
        }
        if (File.Exists(fileName))
           return fileName;
     }
     return null;
  }

Edit: Added link to MS: path.
Edit: Added another link.

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