EDIT:
Ok, as of now and looking through countless posts I this i’ve arrived to a solution, I cannot do what I need to do from a service, I wanted it to be a service so that it would always be on for the computer, but apparently with the way services are written I may not be able to run the commands I want to run because “The service does not run in the same space as the users desktop” so it wont be able to capture the desktop, at least, thats what im gathering so far.
As for now, I’ll leave this here and maybe find a way to start a program under the users name when they logon, to capture the desktop, rather than try and do it from the service directly.
I’ve been banging my head on the wall for a couple hours now trying to figure this out, I’ve never made a service before, just forms applications, well my next project (Client Server Model Client Monitoring utility) needs to be installed as a service on the client machines and send data back to the main server.
Having never written a service before I looked up how to write one and how to install it and whatnot. I got it all working fine, the current working version spits an event log out every 30 seconds to show its working.
I started by running it in LocalSystem because I assumed it had the permissions to write to the file, but after that failed I tried LocalService, that didn’t work either.
Im specifying the Directory as C:\WatcherData\Test.jpg
and tested it in a regular forms application (which works)
OutputImage.Save(“c:\watcherdata\test.jpg”, ImageFormat.Jpeg);
No exceptions, no events, it just doesn’t happen, but the EventLog message is put in immediately before it,so it should be reaching that point, right?
Any assistance would be appreciated, If you need to know anything else I would be happy to oblige
EDIT:
Here goes a little code:
private void ServiceWorkerThread(object state)
{
while (!this.stopping)
{
this.eventLog1.WriteEntry("This is a test2");
CallMe();
Thread.Sleep(30000);
}
this.stoppedEvent.Set();
}
private void CallMe()
{
try
{
int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap OutputImage = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image) OutputImage);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
OutputImage.Save("c:\\watcherdata\\test.jpg", ImageFormat.Jpeg);
}
catch (Exception e)
{
this.eventLog1.WriteEntry(e.Message + "\r\n" + e.StackTrace, EventLogEntryType.Error);
}
}
most of it was taken from a Microsoft sample document, as I said, I’m new to services
currently still trying to figure it out, will let you guys know if i get anywhere
EDIT 2
After playing around some more I managed to get it to write to the file, After realizing that most of the problem was with my lack of knowledge on how to build services and installers, apparently i wasnt rebuilding the installers when i built my service.
Now it just writes all black, a large, black picture. but at least its writing!
How about some source code?
I might add some tracing to make sure the file write code is executed.
Most cases, this type of failure is a permissions issue and/or folks are trying to write to a Windows system directory. You question suggests you are ensuring that you are NOT writing to a Windows Directory. Please make sure c:\watcherdata folder exists.