我想知道在文件开始之前将触发哪个进程:
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/