简单问题:我想使用默认浏览器打开一个 URL,所以我只执行 Process.Start(url)
。但是,我注意到这会返回一个 IDisposable 对象。
所以现在我想知道我是否必须处理它?或者,就此而言,如果我的应用程序以任何方式负责此过程?预期的功能只是“即发即忘”,我不想让我的应用程序成为新进程的父进程,也不需要与之交互。
我在 SO 上看到了一些类似但不相关的问题,这些问题似乎在说简单地在 URL 上调用 Process.Start 就可以了,但我不想遇到一些难以调试的内存泄漏/资源耗尽问题导致我我的程序保留对长期死掉的浏览器进程的引用。
最佳答案
不,你没有。
void Main()
{
Process result = Process.Start("http://www.google.com");
if (result == null)
{
Console.WriteLine("It returned null");
}
}
打印
It returned null
来自 Process.Start Method (String)在 MSDN(.NET Framework 4)上:
If the address of the executable file to start is a URL, the process is not started and null is returned.
(不过,一般来说,using
语句是处理 IDisposable 对象的正确方法。WCF clients 除外。)
关于c# - 我必须处理 Process.Start(url) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1241089/