Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

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.
 
Share this answer
 
Comments
pratyush2008 10-Jan-14 7:16am    
Tnanks Maxim for your reply.

bt how can i sole that problem.
plz guide me.
Maxim Kartavenkov 10-Jan-14 7:27am    
Initialize ServiceProcessInstaller object properties: Account, Password and Username.
pratyush2008 10-Jan-14 8:01am    
Actually am running my service exe in one computer ant it checking disk space of my remote server.
please enplane how to fight with this problem.
Sorry sir for my late understanding.i am quite new in case of window service.
Maxim Kartavenkov 10-Jan-14 10:21am    
The user account from which you currently launching the service does not allows you to perform operation you trying to do in the service, that's why you getting access denied exception. Change the user account from which service will be started as I described.
pratyush2008 11-Jan-14 0:51am    
its not working. i have set : this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.NetworkService; this.serviceProcessInstaller1.Password = "Password";\\my remort server pwd this.serviceProcessInstaller1.Username = "Username";\\my remort server uname
but still am getting same error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900