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

标签 c# file process io

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

Process.Start("PathToFile");

然后我想要进程的路径。

谢谢。

最佳答案

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

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

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

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).

更新

为了在启动进程之前了解可执行文件,您必须查看注册表中的 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;

关于c# - C# 如何知道某个文件将触发哪个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6416765/

相关文章:

c# - 在 Azure DocumentDB 中使用 MongoDB 和 .NET 驱动程序会引发 MongoCommandException

c# - 是否可以在 .NET 中以彩色方式写入控制台?

json - 使用python将json和文件发送到flask

python - 如果文件不存在则复制文件

linux - 当操作系统等待用户输入时,CPU 是否仍在执行任何指令?

android - 有没有办法在 Android 中获取其他第 3 方应用程序的进程 ID...?

c# - 确定渲染字符在 .NET 中的宽度

c# - 如何将新行放入 wpf TextBlock 控件中?

c - 终端 : segmentation fault 11 while reading file and counting the amount of vowels

python - 如何让我的 python 应用程序在继续之前等待某个子进程