Process.Start 返回 null
我正在编写一个程序,在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:感谢 leppie 的评论,我怀疑我知道答案:我的猜测是您正在“启动”类似图像的东西,并且它正在重用现有进程来打开文档而不是创建新文档。
我用这个简单的测试应用程序重现了这一点:
这会打印“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:
This prints "true" because it's using
dllhost.exe
to host the Windows Image Viewer, rather than creating a new process.要解决此问题,您必须将
UseShellExecute
设置为 false 以绕过 shell。而不是
使用
To fix this problem, you have to set
UseShellExecute
to false to bypass the shell.Instead of
use
您在方法范围内声明“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.
那甚至不会编译(明确的分配)。作为方法变量,
proc
仅在声明方法(/scope)中是本地的 - 即button2_Click
,这解释了为什么你不能保留价值观。如果proc
旨在在调用之间保留,请将其提升为字段(每个实例变量):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. Ifproc
is meant to persist between calls, promote it to a field (per-instance variable):