Process.Start 返回 null

发布于 2024-09-14 00:37:26 字数 735 浏览 3 评论 0原文

我正在编写一个程序,在 s 目录中启动一个随机文件。文件可以是任何类型,但主要是视频或图像文件。 每次启动一个文件时,我都想关闭之前打开的文件。

代码:

string FolderSelected = "";
string FileName = "";
Process proc;
            
List<string> FilesDisplayed = new List<string>();

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 的原因,但我需要重写此行为。

I'm writing a program that launches a random file in s directory. the file can be of any type, but mostly video or image files.
Each time I launch a file I want to close the previous opened one.

Code :

string FolderSelected = "";
string FileName = "";
Process proc;
            
List<string> FilesDisplayed = new List<string>();

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);
}

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 that's why it returns Null but I need to override this behavior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心清如水 2024-09-21 00:37:26

编辑:感谢 leppie 的评论,我怀疑我知道答案:我的猜测是您正在“启动”类似图像的东西,并且它正在重用现有进程来打开文档而不是创建新文档。

我用这个简单的测试应用程序重现了这一点:

using System;
using System.Diagnostics;

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

这会打印“true”,因为它使用 dllhost.exe 来托管 Windows 图像查看器,而不是创建新进程。

EDIT: 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);
    }
}

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

乄_柒ぐ汐 2024-09-21 00:37:26

要解决此问题,您必须将 UseShellExecute 设置为 false 以绕过 shell。

而不是

Process.Start("filename", "args")

使用

Process.Start(new ProcessStartInfo() {
    FileName = "filename",
    Arguments = "args",
    UseShellExecute = false
});

To fix this problem, you have to set UseShellExecute to false to bypass the shell.

Instead of

Process.Start("filename", "args")

use

Process.Start(new ProcessStartInfo() {
    FileName = "filename",
    Arguments = "args",
    UseShellExecute = false
});
沦落红尘 2024-09-21 00:37:26

您在方法范围内声明“proc”,因此在该方法顶部检查时它始终当然为空。如果您希望引用存在于函数之外,请将其声明为类级别变量。

您每次(可能)都会生成一个进程,Process.Start 不会返回 null,但当 i 超出范围时,您只是丢失了对它的引用。

You are declaring 'proc' at method scope, so of course it will always be null when checked at the top of that method. If you want the reference to live beyond the function, declare it is a class level variable.

You are spawning a process each time (probably), Process.Start is not returning null, but you simply lose the reference to it when i goes out of scope.

山色无中 2024-09-21 00:37:26

那甚至不会编译(明确的分配)。作为方法变量proc仅在声明方法(/scope)中是本地的 - 即button2_Click,这解释了为什么你不能保留价值观。如果 proc 旨在在调用之间保留,请将其提升为字段(每个实例变量):

Process proc;
private void button2_Click(object sender, EventArgs e)
{
    if (proc != null)
    ...

That won't even compile (definite assignment). As a method variable, proc is local only to the declaring method(/scope) - i.e. button2_Click, which explains why you can't retain values. If proc is meant to persist between calls, promote it to a field (per-instance variable):

Process proc;
private void button2_Click(object sender, EventArgs e)
{
    if (proc != null)
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文