Solved

process.kill in asp.net is access denied

Posted on 2009-07-08
5
2,417 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 IIS and Windows Authentication to SQL 21 54
Tool Box 2 35
Host asp.net pages 5 25
Show/Hide ASP.NET controls with JQuery best practices 3 18
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now