的Process.Start返回null [英] Process.Start returns null

查看:418
本文介绍了的Process.Start返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个启动一个随机文件目录中的程序。该文件可以是任何时间,但大多是视频或图像文件。 $ B $每次我启动的文件,我想关闭之前的b打开之一。
的代码是:

i am writing a program that launches a random file in a directory. the file can be of any time, but mostly video or image files. each time i launch a file i want to close the previous opened one. the code is:

    string FolderSelected = "";
    string FileName = "";
    Process proc;

    List<string> FilesDisplayed = new List<string>();

    public Form1()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (FolderSelected == string.Empty)
            FolderSelected = Properties.Settings.Default.FilesDefaultFolder;

        if (proc != null)
        {
            proc.CloseMainWindow();
            proc.Close();
        }
        FileName = FetchRandomFile();
        proc = Process.Start(FileName);


    }

的问题是,我不断收到PROC = NULL(文件被正确启动),我不能为了关闭它获取先前打开的过程。我知道,.NET重用流程,这就是为什么它返回NULL,但我需要覆盖此行为。
请大家帮忙,
盖伊

the problem is, that i keep getting proc = null (the file is launched properly) and i cannot fetch the previously opened process in order to close it. I know that .NET reuses processes and thats why it returns Null but i need to override this behavior. please help, Guy.

推荐答案

编辑:感谢leppie的评论,我怀疑我知道答:我的猜测是,你在启动类似的图像,它的重用现有的进程中打开而不是创建一个新的文件

Thanks to leppie's comment, I suspect I know the answer: my guess is that you're "starting" something like an image, and it's reusing an existing process to open the document instead of creating a new one.

我受够了这个简单的测试程序转载此:

I've reproduced this with this simple test app:

using System;
using System.Diagnostics;

public class Test
{
    static void Main()
    {
        Process proc = Process.Start("image.tif");
        Console.WriteLine(proc == null);
    }
}

这将打印真,因为它使用 DLLHOST.EXE 举办的Windows图像查看器,而不是创建一个新的进程。

This prints "true" because it's using dllhost.exe to host the Windows Image Viewer, rather than creating a new process.

这篇关于的Process.Start返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆