Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,
I made a desktop application. In my project i use some word and excel file.
when i close the application some "Winworld.exe" is still running in task manager.
and when i close the application then if i want to delete that particular word file it can't delete.I want to release the memory.

Thanks in Advance.
Posted

You should always track opened word and excel applications to close them when your application exits.

This is the code to release the word or excel application. This is for word application but change and use it for excel applications :
C#
wordApplication.Quit(SaveChanges: false, OriginalFormat: false, RouteDocument: false);

System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApplication);


If your application was crashed and you didn't have the chance to close the excel or word application you should use this special code :

Helper class :
C#
public static class ProcessExtension
{
    private static string FindIndexedProcessName(int pid)
    {
        try
        {
            var processName = Process.GetProcessById(pid).ProcessName;

            var processesByName = Process.GetProcessesByName(processName);

            string processIndexdName = null;

            for (var index = 0; index < processesByName.Length; index++)
            {
                processIndexdName = index == 0 ? processName : processName + "#" + index;

                var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);

                if ((int)processId.NextValue() == pid)
                {
                    return processIndexdName;
                }
            }

            return processIndexdName;
        }
        catch
        {
            return "";
        }
    }

    private static Process FindPidFromIndexedProcessName(string indexedProcessName)
    {
        var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);

        try
        {
            return Process.GetProcessById((int)parentId.NextValue());
        }
        catch
        {
            return null;
        }
    }

    public static Process Parent(this Process process)
    {
        return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
    }
}

Some parts of this code were extracted from stackoverflow.

And use it by this way :
C#
Process parentProcess;

foreach (var process in Process.GetProcesses().Where(i => i.ProcessName == "WINWORD"))
{
    parentProcess = process.Parent();

    if ((parentProcess != null) && (string.Compare(parentProcess.ProcessName, "svchost", true) == 0))
    {
        process.Kill();
    }
}

This will kill all WORD processes which their parent is svchost .

If user starts an excel or word application its parent will be explorer.

Change it so that it can kill excel processes.

Hope it helps.
 
Share this answer
 
Comments
BillWoodruff 28-Jan-12 10:51am    
Looks like a good solid answer: the one question I have is: if the end-user had both an instance of Word, or Excel, launched, and was using Word, or Excel, within a .Net application: would your solution close all of the instances of Word ?
Amir Mahfoozi 28-Jan-12 13:28pm    
Dear Bill, I have tested this code with two excel processes: one was started by a .NET application and the other was started by clicking on an .xls file. this code successfully killed the .NET started excel and left the user started one intact. However there are other conditions that can make this code seem silly! This code fulfilled our needs to some extents but if someone can define a not considered situation, of course he can develop its solution too. Thank you for your consideration again.
BillWoodruff 28-Jan-12 17:42pm    
A very satisfying answer to a perhaps picky-picky question, but one asked out of true curiosity :) I have voted your answer the +5 it deserves !
Amir Mahfoozi 29-Jan-12 0:03am    
Thank you Bill :)
Sergey Alexandrovich Kryukov 28-Jan-12 17:08pm    
Very good, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900