我尝试从我的C#代码在Adobe Reader中打开2个pdf文件。让我们称他们为A和B,然后A在B之前打开。如何从C#关闭PDF文件#
现在,当我尝试杀死与文件A关联的进程时,文件B也关闭,因为它们链接到同一进程。是否有办法关闭文件A而不关闭文件B.
另外,当我第一次尝试杀死与文件B相关的进程时,没有任何反应,并且文件B仍然保持打开状态。
我该如何着手解决上述两种情况。
我有这两个文件的句柄。有没有一种方法可以关闭手柄
我尝试从我的C#代码在Adobe Reader中打开2个pdf文件。让我们称他们为A和B,然后A在B之前打开。如何从C#关闭PDF文件#
现在,当我尝试杀死与文件A关联的进程时,文件B也关闭,因为它们链接到同一进程。是否有办法关闭文件A而不关闭文件B.
另外,当我第一次尝试杀死与文件B相关的进程时,没有任何反应,并且文件B仍然保持打开状态。
我该如何着手解决上述两种情况。
我有这两个文件的句柄。有没有一种方法可以关闭手柄
听起来对我来说,您应该使用Acrobat的Interapplication Communication API,它具有打开和关闭文档的功能。与IAC(pdf documentation here)相比,你所做的相当不雅。
我认为一种方法是找到该程序实例并从应用程序中关闭它。这里是一个如何找到窗口并关闭它的例子:http://www.mycsharpcorner.com/Post.aspx?postID=32
既然你有2个Adobe reader打开的实例,你会想确定哪个是哪个。您可以通过框架中的文字进行搜索。如果你有一个spy ++(或类似的替代品)的副本,它使得外部GUI组件的工作变得更加容易,因为你可以找到那么多关于该窗口的信息,包括名称,窗口句柄等等。
您可以通过以下代码找到A的PDF查看器的过程。
using System.Diagnostics;
public bool FindAndKillProcess(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses()) {
//now we're going to see if any of the running processes
//match the currently running processes by using the StartsWith Method,
//this prevents us from incluing the .EXE for the process we're looking for.
//. Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running
if (clsProcess.ProcessName.StartsWith(name))
{
//since we found the proccess we now need to use the
//Kill Method to kill the process. Remember, if you have
//the process running more than once, say IE open 4
//times the loop thr way it is now will close all 4,
//if you want it to just close the first one it finds
//then add a return; after the Kill
clsProcess.Kill();
//process killed, return true
return true;
}
}
//process not found, return false
return false;
}
然后调用上面的方法。
FindAndKillProcess(“AcroRd32.exe”);
所以你可以杀死PDF阅读器的过程。
这将杀死它找到的第一个Acrobat Reader,这将关闭文件A,文件B或两者 - 谁知道?另外,对于OP而言,比对Brijesh更多,您如何知道最终用户使用的是Adobe而不是其他某些PDF查看器?我们真的需要知道如何启动PDF以知道如何杀死它。 – 2012-03-21 12:43:38
@brijesh但是这也会关闭文件b,因为相同的进程与文件B相关联。我不想让文件b关闭。 – 2012-03-21 12:45:12
好吧,我明白了。有解决方案,您需要先关闭A,然后才打开B.表示一次只打开一个PDF。所以没有问题。 – 2012-03-21 13:21:52
TRY:
如果(clsProcess.ProcessName 包含(名称)。)
INSTEAD:
如果(clsProcess.ProcessName StartsWith(名称))
using System.Diagnostics;
public bool FindAndKillProcess(string name)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Contains(name))
{
//To know if it works
//MessageBox.Show(clsProcess);
clsProcess.Kill();
return true;
}
}
//process not found, return false
return false;
}
////// call the function:
FindAndKillProcess("AcroRd32");
////// if you have been saved all the variables also you can close you main form
FindAndKillProcess("Form_Name");
发布启动pdf并杀死它的代码 – 2012-03-21 12:30:01