Solved

process.kill in asp.net is access denied

Posted on 2009-07-08
5
2,601 Views
Last Modified: 2012-06-27
In the following code, this is my excel process kill for clearing memory after use by asp.net. It has worked before I change my IP address, after that there're some error about "System.ComponentModel.Win32Exception: Access is denied" at proc.Kill(). When I change my IP to the old one, it work, but I have to using new IP. So please give me some idea about this.
For Each proc As Process In Process.GetProcessesByName("EXCEL")
     proc.Kill()
 Next

Open in new window

0
Comment
Question by:KrichayaE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:ppittle
ID: 24804305
From the Process.Kill documentation http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill(VS.80).aspx:

If the call to the Kill method is made while the process is currently terminating, a Win32Exception is thrown for Access Denied.

Try using proc.WaitForExit() http://msdn.microsoft.com/en-us/library/ty0d8k56(VS.80).aspx.  This does a synchronus close, meaning your program will halt execution until the Excel process has closed.  The Kill method is aynschronus, meaning that your code will not wait for Excel to close.  If the Kill method is called for the same process more than once, an Exception will be thrown.

Also, the Execution Context at the time when proc.Kill or proc.WatForExit() executes needs to have full trust permission.  One way to do this is with WindowsImpersonationContext:

using (WindowsImpersonationContext impCtx = (HttpContext.Current.User.Identity as WindowsIdentity).Impersonate())
{
  //Kill process code
 
  impCtx.Undo();
}

0
 

Author Comment

by:KrichayaE
ID: 24810393
I found my jerky cause, it come from Excel File opening from outside asp.net. So "Process.GetProcessesByName("EXCEL")" is include my Excel File into process and surely that it will access denied.

In the other way, I try to using "proc.WaitForExit()" like ppittle suggest. It will waiting until timeout if I not closing this Excel File.

So I have an idea that "Process.GetProcessesByName("EXCEL")" should get process by user name but I don't know how to. Are there any way that "Process.GetProcessesByName("EXCEL")" will check process from user "ASPNET" only ?
0
 
LVL 8

Accepted Solution

by:
ppittle earned 500 total points
ID: 24842156
Give this a try:

//Iterate through all Excel processes on local machine
foreach (Process proc in Process.GetP{rocessesByName("EXCEL"))
{
   //Only work with Processes started by ASPNET
   if (proc.StartInfo.UserName.Equals("ASPNET", StringComparison.InvariantCultureIgnoreCase))
   {
            //Impersonate a user that has permissions to kill the process
            using (WindowsImpersonationContext impCtx = (HttpContext.Current.User.Identity as    WindowsIdentity).Impersonate())
{
  //Kill process code
 
  impCtx.Undo();
}

    }
}
0
 

Author Comment

by:KrichayaE
ID: 24855912
Yah! It work. Thanks a million, ppittle.
0
 
LVL 8

Expert Comment

by:ppittle
ID: 24859935
I'm glad to hear the solution worked for you.  Happy coding!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question