Solved

process.kill in asp.net is access denied

Posted on 2009-07-08
5
2,372 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yah! It work. Thanks a million, ppittle.
0
 
LVL 8

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

11 Experts available now in Live!

Get 1:1 Help Now