我想知道在启动文件之前将触发哪个进程:
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.
最满意答案
您可以查看从Process.Start返回的Process的MainModule属性:
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.exeHowever, 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;如何知道c#中某个文件将触发哪个进程(How to know which process will be fired by a certain file in c#)我想知道在启动文件之前将触发哪个进程:
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.
最满意答案
您可以查看从Process.Start返回的Process的MainModule属性:
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.exeHowever, 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;
发布评论