自定义Windows服务错误:无法在计算机上启动<servicename>服务。'</servicename>

时间:2014-03-04 09:41:41

标签: windows powershell service

我写了一个自定义Windows服务来备份两个网络磁盘之间的文件。我正在尝试在Windows服务器2012上运行此服务。该服务安装得很好,但是当我尝试启动它时它会遇到此错误:无法在计算机上启动服务'。'。 这是我的代码:

public partial class OptiekBackupService : ServiceBase
{
    private System.Threading.Timer aTimer;

    public OptiekBackupService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("OptiekBackupSource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "OptiekBackupSource", "OptiekBackupLog");
        }
        EventLogger.Source = "OptiekBackupSource";
        EventLogger.Log = "OptiekBackupLog";
    }

    protected override void OnStart(string[] args)
    {
        EventLogger.WriteEntry("OptiekBackupService Started");
        double TimeOfExecution = 6;
        DateTime now = DateTime.Now;
        DateTime today5am = now.Date.AddHours(TimeOfExecution);
        DateTime next5am = now <= today5am ? today5am : today5am.AddDays(1);

        System.Threading.TimerCallback callback = new System.Threading.TimerCallback(RunBackup);

        this.aTimer = new System.Threading.Timer(callback, null, next5am - DateTime.Now, TimeSpan.FromHours(24));
        //new System.Threading.Timer(callback, null, next5am - DateTime.Now, TimeSpan.FromHours(24));
    }

    protected override void OnStop()
    {
        EventLogger.WriteEntry("OptiekBackupService Stopped");
    }

    public void RunBackup(Object stateInfo)
    {
        EventLogger.WriteEntry("Backup started");
        RecursiveLookUp("X:\\", "W");
        RecursiveLookUp("Z:\\", "Y");
        EventLogger.WriteEntry("Backup completed");
    }
    //Recursieve methode om bestanden te doorlopen. 
    void RecursiveLookUp(string Loc, string Dest)
    {
        //Console.WriteLine("We bevinden ons hier :" + Loc);
        /*if (Directory.GetDirectories(Loc).Length == 0)
        {
            CopyFiles(Loc, Dest);
            Console.WriteLine("Stop einde van filestructuur bereikt.");
        }
        else
        {*/
            string[] dirs = Directory.GetDirectories(Loc);
            foreach (var item in dirs)
            {
                RecursiveLookUp(item, Dest);
            }
            CopyFiles(Loc, Dest);
        //}
    }

    static void CopyFiles(string Loc, string Dest)
    {
        string[] files = Directory.GetFiles(Loc);
        //Create directory hier
        string NewLocDir = Dest + Loc.Substring(1);
        System.IO.Directory.CreateDirectory(NewLocDir);
        //Console.WriteLine(NewLoc);
        foreach (var item in files)
        {
            //Console.WriteLine(item);
            string NewLocFile = Dest + item.Substring(1);
            if (File.Exists(NewLocFile))
            {
                if (File.GetLastWriteTime(NewLocFile) < File.GetLastWriteTime(item))
                {
                    System.IO.File.Delete(NewLocFile);
                    System.IO.File.Copy(item, NewLocFile);
                    //Console.WriteLine("Copy to: " + NewLocFile + " Completed");
                }
                /*else
                {
                    //Console.WriteLine("File is up to date no copy needed");
                }*/
            }
            else
            {
                System.IO.File.Copy(item, NewLocFile);
            }
        }
    }
}

我使用powershell进行安装和启动。

当我尝试在服务器管理器中启动服务时,它也没有启动,所以我怀疑问题出在这个问题上。

也许这是我在VS项目属性中遗忘的某个地方?

我遵循了这个:MSDN Walkthrough

0 个答案:

没有答案