Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one
Now i want to Close a application based on my file name. I m creating a PDF file when i create it opens.But after creating it i need to send to client sys so when i read that application (using Binary reader) it shows error because the file s open So now i need to close that PDF only which i want .
Example: If i open a.pdf and b.pdf

now i need to close a.pdf .But when i use the code below
C#
foreach (Process p in Process.())
            {
                p.Kill();
                
            }



i will get all the process . but when i give to kill a.pdf it will kill both a.pdf and b.pdf
Posted

Get the Process Id and kill the process related to that id.

First use
C#
Process.GetProcessById(Int32.Parse(id));
to get the process and then kill it.
See here[^] for an example.
 
Share this answer
 
Hi,

here the sample code it will kill only the excel process which are created at run time of that application.

To Collect the excel process id's

Hashtable objhashProcess = new Hashtable();
Hashtable objHashProcess1 = new Hashtable();

protected void CheckExcelProcess(int call)
    {
        try
        {
            if (call == 1)
            {
                int iCount = 0;
                Process[] ps = Process.GetProcesses();
                foreach (Process p in ps)
                {
                    if (p.ProcessName.ToLower().Equals("excel"))
                    {
                        objhashProcess.Add(p.Id, iCount);
                        iCount++;
                    }
                }
            }
            else if (call == 2)
            {
                int icount = 0;
                Process[] ps = Process.GetProcesses();
                foreach (Process p in ps)
                {
                    if (p.ProcessName.ToLower().Equals("excel"))
                    {
                        if (!objhashProcess.Contains(p.Id))
                        {
                            objHashProcess1.Add(p.Id, icount);
                            icount++;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }


Here to kill the process which are opened by the application:
protected void killProcess()
    {
        try
        {
            Process[] ps = Process.GetProcesses();
            foreach (Process p in ps)
            {
                if (p.ProcessName.ToLower().Equals("excel"))
                {
                    if (objHashProcess1.Contains(p.Id))
                    {
                        p.Kill();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
        }
    }


Thanks..
 
Share this answer
 
Try this, but here the title of each pdf should be different.
C#
string bookTitle = "titleOfThePdfToBeRemoved"; // The pdf title that you want to close.
 Process[] AllProcesses = Process.GetProcessesByName("pdf");
 foreach (var process in AllProcesses)
 {
    string tempTitle = process.MainWindowTitle.Split('-')[1].TrimStart();
    if (bookTitle == tempTitle)
        {
            process.Kill();
        }
 }
 
Share this answer
 
Comments
I.explore.code 21-Oct-11 10:05am    
doesn't work mate!! not on my office machine anyway...

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