Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何知道c中的某个文件将触发哪个进程#_C#_File_Process_Io - Fatal编程技术网

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

C# 如何知道c中的某个文件将触发哪个进程#,c#,file,process,io,C#,File,Process,Io,我想知道在启动文件之前将启动哪个进程: Process.Start("PathToFile"); 然后我想知道流程的路径 谢谢。它返回一个包含更多信息的进程对象。MainModule可能是适合您的属性 编辑: 你想先知道/不想知道吗 开始这个过程-对 您可以在注册表中查找已注册的文件处理程序-for.doc、.txt等。您可以查看从以下位置返回的进程的MainModule属性: 但是,您应该记住Proces.Start的返回值可能为null-根据MSDN,返回值为: 一个新的流程组件 与流

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

Process.Start("PathToFile");
然后我想知道流程的路径

谢谢。

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

编辑:


你想先知道/不想知道吗 开始这个过程-对


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

您可以查看从以下位置返回的进程的
MainModule
属性:

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

一个新的流程组件 与流程资源关联, 或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;
要使用windows文件关联打开的文档 我在这里找到了这个链接,它解释了如何创建文件关联。当然,您需要阅读注册表。我知道有两种格式

您不知道其路径的程序 path环境变量在当前目录之后作为默认路径进行查询,以便在未提供路径时查找。 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;
  }
publicstaticstringgetpath(stringpathtofile)
{
string filename only=Path.GetFileName(pathToFile);
List folders=Environment.GetEnvironmentVariable(“Path”).Split(“;”).ToList();
文件夹.Insert(0,Environment.CurrentDirectory);
foreach(文件夹中的字符串文件夹)
{
字符串文件名;
尝试
{
//无法信任Path环境变量是否正确构造。
fileName=Path.Combine(文件夹,仅限文件名);
}
抓住
{
继续;
}
if(File.Exists(fileName))
返回文件名;
}
返回null;
}
编辑:添加链接到。
编辑:添加了另一个。

是否希望在启动进程之前/不启动进程时了解它?是否指定实际路径(“%systemroot%\system32”)或无路径程序名(如“notepad.exe”)?是否尝试运行类似notepad的程序?或者你想打开一个文档,比如recipes.doc?@agent-j.doc、.docx和.pdf我唯一能想到的解决方案就是读取注册表项并模仿shell逻辑。这显然是邪恶的。邪恶,但我想不出更好的了。谢谢。我认为你的答案在大多数情况下都应该有效。但在我的情况下,注册表并不能准确反映.Start()将启动的进程。我在我的电脑上安装了OfficeWord2010,然后我安装了Word2007来测试一些东西。在注册表中,.doc和docx应该用word 2007打开,但它是用word 2010打开的。。。真奇怪。“但你的回答绝对值得更多的赞誉。”琼,对不起。在我理解您正在打开文件之前,我编写了此代码。我以为你在运行一个程序,但你不知道它的路径。@Jean,我不知道这个程序有什么灵丹妙药。我添加了一个链接,你应该看看。
  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;
  }