我可以看到许多非常相似的线索,但似乎没有给我一个解决方案,应该是非常基本的。
在我的winforms应用程序中,我需要关闭一个正在运行的word文档实例(从应用程序本身打开)。打开应用程序中的word文档时,我会将其记录在列表中。现在我怎么才能关闭同一个医生?
以下是我尝试过的:
private bool CloseWord(string osPath) //here I pass the fully qualified path of the file
{
try
{
Word.Application app = (Word.Application)Marshal.GetActiveObject("Word.Application");
if (app == null)
return true;
foreach (Word.Document d in app.Documents)
{
if (d.FullName.ToLower() == osPath.ToLower())
{
d.What? //How to close here?
return true;
}
}
return true;
}
catch
{
return true;
}
}对于这个文档对象,我得到了很多方法,但是只有一个要关闭的.Close(),它有类似于这样的参数:ref object SaveChanges, ref object OriginalFormat, ref object RouteDocument (我不明白)。
最理想的方法是什么?谢谢..
编辑:
Process.Kill()),而不需要任何提示用户保存或不保存等等。发布于 2011-09-16 15:01:30
这个从here得到的解决方案解决了。
Word.Application app = (Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
if (app == null)
return true;
foreach (Word.Document d in app.Documents)
{
if (d.FullName.ToLower() == osPath.ToLower())
{
object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
object originalFormat = Word.WdOriginalFormat.wdOriginalDocumentFormat;
object routeDocument = false;
d.Close(ref saveOption, ref originalFormat, ref routeDocument);
return true;
}
}
return true;发布于 2011-09-16 14:22:59
不如使用这样的方法:
修改自:http://www.dreamincode.net/code/snippet1541.htm
public static bool KillProcess(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses())
{
if (Process.GetCurrentProcess().Id == clsProcess.Id)
continue;
//now we're going to see if any of the running processes
//match the currently running processes. 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.
//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
if (clsProcess.ProcessName.Contains(name))
{
clsProcess.Kill();
return true;
}
}
//otherwise we return a false
return false;
}发布于 2012-11-14 08:53:07
我知道为时已晚,但我发现这个话题是因为我有同样的问题。
我的解决方案可能会对其他人有所帮助。
上面的解决方案效果不佳,因为它杀死了Word的每个运行实例,尽管它不是由c#程序打开的,而是由用户打开的。
所以这并不是那么友好的用户。
我的代码检查Word.Application对象在c#中创建之前/之后的进程,并在List<int>中写入WINWORD进程的in,然后比较两个List<int>中的值。当在两个processID中都找不到一个List<int>时,它就会用这个ID杀死进程。
public List<int> getRunningProcesses()
{
List<int> ProcessIDs = new List<int>();
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses())
{
if (Process.GetCurrentProcess().Id == clsProcess.Id)
continue;
if (clsProcess.ProcessName.Contains("WINWORD"))
{
ProcessIDs.Add(clsProcess.Id);
}
}
return ProcessIDs;
}运行这两次(一次在创建Word.Application之前,一次在创建之后)。
List<int> processesbeforegen = getRunningProcesses();
// APP CREATION/ DOCUMENT CREATION HERE...
List<int> processesaftergen = getRunningProcesses();然后运行killProcesses(processesbeforegen, processesaftergen);
private void killProcesses(List<int> processesbeforegen, List<int> processesaftergen)
{
foreach (int pidafter in processesaftergen)
{
bool processfound = false;
foreach (int pidbefore in processesbeforegen)
{
if (pidafter == pidbefore)
{
processfound = true;
}
}
if (processfound == false)
{
Process clsProcess = Process.GetProcessById(pidafter);
clsProcess.Kill();
}
}
}https://stackoverflow.com/questions/7445831
复制相似问题