Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, November 22, 2012 12:49 PM
0
Hello,
I have this code for my WindowsServicfe, which should copy one folder to another folder on other computer.
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace ServiceBack
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//System.Diagnostics.Debugger.Launch();
string fileName = "filename.mdb";
string sourcePath = @"Z:";
string targetPath = @"C:\TelPov";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
DateTime dt = DateTime.Today;
int month = dt.Month;
string mesec = Convert.ToString(month) + ".mdb";
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = Directory.GetFiles(sourcePath, "*" + mesec);
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Ne postoi izvornata pateka!");
}
}
protected override void OnStop()
{
}
}
}
I set the serviceProcessInstaller1 Account property to LocalSystem.
But when I started the service I got this message:
The "service name" service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other service or programs
Can anybody help me please what is wrong with my Windows Service?
Thanks in advance.
P.S. When I put this code in a Windows Form, it works fine.
All replies (4)
Thursday, November 22, 2012 2:01 PM âś…Answered
you can go to the services.msc from run, and see what user is present for that service that you have created. It should be "Local System Account" under which the service should be running when you right click on the service and see the properties or check this link http://stackoverflow.com/questions/538925/windows-service-stops-automatically
regards
joon
Thursday, November 22, 2012 2:16 PM | 1 vote
A form project will have components to initialize. A service doesn't have a form which is probably giving you the error. I put your code into a console application and removed the intiailiztion of the form. I also added exception handlers and a log file to help you locate problems wiht the application
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using System.IO;namespace ServiceBack{ public partial class Service1 : ServiceBase { static void Main(string[] args) { try { FileStream error = File.Open(@"c:\temp\logfile.log", FileMode.Append); string currentTime = DateTime.Now.ToLongDateString(); string errorMessage = currentTime + "Service Started\n"; error.Write(errorMessage, 0, errorMessage.Length); error.Flush(); error.Close(); //System.Diagnostics.Debugger.Launch(); string fileName = "filename.mdb"; string sourcePath = @"Z:"; string targetPath = @"C:\TelPov"; string sourceFile = System.IO.Path.Combine(sourcePath, fileName); string destFile = System.IO.Path.Combine(targetPath, fileName); if (!System.IO.Directory.Exists(targetPath)) { System.IO.Directory.CreateDirectory(targetPath); } System.IO.File.Copy(sourceFile, destFile, true); DateTime dt = DateTime.Today; int month = dt.Month; string mesec = Convert.ToString(month) + ".mdb"; if (System.IO.Directory.Exists(sourcePath)) { string[] files = Directory.GetFiles(sourcePath, "*" + mesec); foreach (string s in files) { fileName = System.IO.Path.GetFileName(s); destFile = System.IO.Path.Combine(targetPath, fileName); System.IO.File.Copy(s, destFile, true); } } else { Console.WriteLine("Ne postoi izvornata pateka!"); } } catch (Exception e) { FileStream error = File.Open(@"c:\temp\logfile.log",FileMode.Append); string currentTime = DateTime.Now.ToLongDateString(); string errorMessage = currentTime + " Error : " + e.ToString() + "\n"; error.Write(errorMessage, 0, errorMessage.Length); error.Flush(); error.Close(); } } }}
jdweng
Thursday, November 22, 2012 4:50 PM | 1 vote
A windows service pretty much act like a console application. The OnStart method fire when the service is started go through your code and since there is nothing left to do the application shutdown (and you get the message error).
You need to have a mechanism to like a timer or FileWatcher to trigger the functionality of your windows service.
What is the purpose of your service?
Friday, November 23, 2012 1:30 PM
It's for making backup of of one folder to another