出现错误System.ComponentModel.win32Exception:访问被拒绝 [英] Getting Error System.ComponentModel.win32Exception: Access is Denied
问题描述
嗨
我有Windows服务.有时候我启动服务时会出现异常
I have a windows service. some times when i start the service i am getting the exception
System.ComponentModel.Win32Exception:访问被拒绝在System.Diagnostics.ProcessManager.OpenProcess(Int32进程ID,Int32访问,布尔throwIfExited)处进行操作.在System.Diagnostics.Process.GetProcessHandle处(Int32访问,布尔throwIfExited). 在System.Diagnostics.Process.GetProcessTimes()处在System.Diagnostics.Process.get_StartTime()
System.ComponentModel.Win32Exception: Access is denied at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited) at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited) at System.Diagnostics.Process.GetProcessTimes() at System.Diagnostics.Process.get_StartTime()
该服务的代码为:-
受保护的
无效 OnStart( [] args)
override void OnStart(string[] args)
{
Process []进程= Process.GetProcessesByName("XYZ");
if(处理!= null)
{
b foreach(流程中的流程)
b {
Process[] processes = Process.GetProcessesByName("XYZ");
if (processes != null)
{
foreach (Process process in processes)
{
b&b ;
nbsp; bsp ProcessID = process.Id;
nbsp; bsp
nbsp; bsp ProcessPageSize = process.PagedMemorySize64.ToString();
ProceeTime = process.StartTime.ToString();
nbsp; bsp 进程数= process.Threads.Count;
nbsp; bsp ProceesAffinity = process.ProcessorAffinity.ToString()
nbsp; bsp
nbsp; bsp 如果(Process.GetCurrentProcess().Id!= process.Id)
nbsp; bsp {
nbsp; bsp process.Kill();
nbsp; bsp
nbsp; bsp }
b }
}
ProcessID = process.Id;
ProcessPageSize = process.PagedMemorySize64.ToString();
ProceeTime = process.StartTime.ToString();
Processcount = process.Threads.Count;
ProceesAffinity = process.ProcessorAffinity.ToString()
if (Process.GetCurrentProcess().Id != process.Id)
{
process.Kill();
}
}
}
因此,整个想法是,当winodws服务启动时,它会创建一个进程"XYZ".和m试图杀死任何以前存在的OLD xyz进程.
So the whole idea is when the winodws service starts , it creates a process "XYZ" and m trying to kill any OLD xyz process if it is previously present.
再次出现错误时,重新启动系统后,Windows服务将开始正常运行.该服务在aacount帐户下运行,该帐户在盒子上有管理员权限.
Again whenever the error comes, after restarting the system the windows service is started working fine. The service is running under the aacount which has admin prvilage on the box.
如果有人可以提供指向它的指针,那将是很合适的.
Would appriciate, if anybody could provide any pointers to it.
推荐答案
尝试获取堆栈跟踪或异常信息以查看异常的来源.
try getting the stack trace or the exception info to see where the exception comes from.
另外,可能是您正在更改迭代器,所以您要做的是(如果这是问题所在):
Also, it might be that you're changing the iterator, so what you have to do is (in case this is the problem):
int processId = Process.GetCurrentProcess().Id;
List<int> processToKill = new List<int>();
processToKill.AddRange(
Process.GetProcessesByName("XYZ")
.Where(p => p.Id != processId)
.Select(p => p.Id)
);
processToKill.ForEach(p => Process.GetProcessById(p).Kill());
这篇关于出现错误System.ComponentModel.win32Exception:访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!