我怎样才能获得用于在C#中打开文件的进程

时间:2016-03-21 14:44:15

标签: c# wpf windows

我正在尝试使用与之关联的应用程序打开不同的文件:

                ProcessPath = @"C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg";
                ProcessStartInfo processStartInfo = new ProcessStartInfo();
                processStartInfo.FileName = ProcessPath;
                processStartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(ProcessPath);

                process = new Process();
                process.StartInfo = processStartInfo;
                process.EnableRaisingEvents = true;
                process.Exited += Process_Exited;
                process.Start();     

请问我能否帮助我掌握代表应用程序打开的Process对象,因为在调用process.Start()之后;进程中的大多数字段都抛出异常。

感谢。

1 个答案:

答案 0 :(得分:0)

我设法找到了解决方案:

使用Windows API函数

        [DllImport("shell32.dll")]
    static extern int FindExecutable(string file, string directory, [Out] StringBuilder lpResult);

返回给定文件的关联应用程序路径。然后我可以使用我发布的原始代码执行该关联的应用程序,并将原始文件作为参数传递!