Solved

process.kill in asp.net is access denied

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

636 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