窗口服务显示错误 [英] Window Service showing erore

查看:84
本文介绍了窗口服务显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写服务以检查我的服务器磁盘空间。



代码如下:

protected override void OnStart(string [] args)

{BackUpDispSpace();}

protected void BackUpDispSpace()

{

EXESpec = Application.ExecutablePath;

TXTSpec = EXESpec.Substring(0,EXESpec.Length - 3)+txt;



ConnectionOptions oConn = new ConnectionOptions();

oConn.Username =Username;

oConn.Password =Password;

string Drive = ;

string FreeSpace =;

string TotalSpace =;

int UsedspacePercent = 0;

string strNameSpace = @\\localhost\root\cimv2;

尝试

{

ManagementScope oMs = new ManagementScope( strNameSpace, oConn);

ObjectQuery oQuery = new ObjectQuery(选择FreeSpace,大小,来自Win32_LogicalDisk的名称,其中DriveType = 3);

ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery) );

ManagementObjectCollection oReturnCollection = oSearcher.Get();

foreach(管理对象oReturn in oReturnCollection)

{

Drive = oReturn [Name]。ToString();

if(Drive ==H:||开车==我:)

{

FreeSpace = BytesToString(Convert.ToInt64(oReturn [FreeSpace]));

TotalSpace = BytesToString(Convert.ToInt64(oReturn [Size]));

long UsedSpace = Convert.ToInt64(oReturn [Size]) - Convert.ToInt64(oReturn [FreeSpace] );

UsedspacePercent = Percentval(Convert.ToInt64(oReturn [Size]),UsedSpace);

if(UsedspacePercent> = 60)

{

字符串Subject =BackUp Drive(+ Drive +)完全填满(+ UsedspacePercent +%Used)。;

string Body = BodyDiskSpace(localhost,Drive,FreeSpace);

MailService.SendMail(pratyushbiswal2008@gmail.com,主题,正文);

}

} < br $>
}

}

catch(例外情况)

{

;

}

}



我的问题是当我在窗口表单级别测试此代码时工作正常。但是当我把这个代码放在我的窗口服务级别时它显示错误。

错误信息是:

错误:System.UnauthorizedAccessException:访问被拒绝。 (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))

在System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo)

在System.Management.ManagementScope。 System.Management.ManagementScope.Initialize()中的InitializeGuts(Object o)



在System.Management.ManagementObjectSearcher.Initialize()

at System.Management.ManagementObjectSearcher.Get()

at DataLogClear.DataLogClear.BackUpDispSpace()



任何人都可以请帮助我。谢谢提前。

I am writing a service to check my server disk space.

the code are below:
protected override void OnStart(string[] args)
{BackUpDispSpace();}
protected void BackUpDispSpace()
{
EXESpec = Application.ExecutablePath;
TXTSpec = EXESpec.Substring(0, EXESpec.Length - 3) + "txt";

ConnectionOptions oConn = new ConnectionOptions();
oConn.Username = "Username";
oConn.Password = "Password";
string Drive = "";
string FreeSpace = "";
string TotalSpace = "";
int UsedspacePercent = 0;
string strNameSpace = @"\\localhost\root\cimv2";
try
{
ManagementScope oMs = new ManagementScope(strNameSpace, oConn);
ObjectQuery oQuery = new ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
foreach (ManagementObject oReturn in oReturnCollection)
{
Drive = oReturn["Name"].ToString();
if (Drive == "H:" || Drive == "I:")
{
FreeSpace = BytesToString(Convert.ToInt64(oReturn["FreeSpace"]));
TotalSpace = BytesToString(Convert.ToInt64(oReturn["Size"]));
long UsedSpace = Convert.ToInt64(oReturn["Size"]) - Convert.ToInt64(oReturn["FreeSpace"]);
UsedspacePercent = Percentval(Convert.ToInt64(oReturn["Size"]), UsedSpace);
if (UsedspacePercent >= 60)
{
string Subject = "BackUp Drive(" + Drive + ") Is Allmost Full(" + UsedspacePercent + " % Used).";
string Body = BodyDiskSpace("localhost", Drive, FreeSpace);
MailService.SendMail("pratyushbiswal2008@gmail.com", Subject, Body);
}
}
}
}
catch (Exception ex)
{
;
}
}

my problem is when i was tested this code in my window form level it was working fine.but when i placed this code in my window service level it showing error.
Error message is:
Error : System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementScope.InitializeGuts(Object o)
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementObjectSearcher.Initialize()
at System.Management.ManagementObjectSearcher.Get()
at DataLogClear.DataLogClear.BackUpDispSpace()

Any one can please help me.Thanks in advance.

推荐答案

您有访问权限。

Windows服务可以作为指定用户执行,只需在服务启动时指定用户信息。通常,服务作为LocalSystem或LocalService启动,并且在服务当前用户中启用,并且不存在权限。但是服务可以从指定用户启动并使用它权限。

关于这一点,一些访问当前用户设置可能有所不同,例如当前用户路径或用户注册表返回不同的值。



Maxim。
You have access denined exception.
Windows Service can be executes as specified user, just specify user information while service starting. Usually services started as LocalSystem or LocalService and in service current user and it rights not present. But services can be started from specified user and uses it rights.
Regarding that, some accessing current user settings may works differ for example Current user paths or user registry returns different values.

Maxim.


这篇关于窗口服务显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆