Windows服务没有在c#中触发

时间:2016-01-11 08:42:23

标签: c# windows-services

我一直运行Windows服务超过一年,将文件从一个文件夹复制到另一个文件夹。它将时间写入文本文件,并在每个时间间隔后检查文本文件中写入文件的时间必须复制,如果要复制的文件大于文本文件中写入的时间,则将被复制。它现在停止工作我试图删除该服务并从visual studio命令提示符重新安装,但它不起作用。

 public partial class FileService : ServiceBase
{
    Timer aTimer = new Timer();
    public FileService()
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
        aTimer.Enabled = true;
        aTimer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        aTimer.Interval = 60000;
        aTimer.Start();
    }
    private void CopyFiles()
    {
        try
        {
            string ReadTextFile=File.ReadAllText(@"\\10.0.0.0\c$\Database\tempFolder\date.txt");
            DateTime TimeInTextFile = Convert.ToDateTime(ReadTextFile);

            if (File.GetLastWriteTime(@"\\10.19.0.1\folder\file.dbf") > TimeInTextFile)
            {
                File.Copy(@"\\10.19.0.1\folder\file.dbf", @"\\10.0.0.0\c$\Database\tempFolder" + "\\_file.dbf", true);                  
                StreamWriter WriterTotxt = new StreamWriter(@"\\10.0.0.0\c$\Database\tempFolder\date.txt, false);
                DateTime LastVipAccess = File.GetLastWriteTime(@"\\10.19.0.1\folder\file.dbf");
                WriterTotxt.Write(LastVipAccess);
                WriterTotxt.Flush();
                WriterTotxt.Close();
                RenameAndMoveFiles();
                System.Threading.Thread.Sleep(5000);
                string[] RenamedDbfFiles = Directory.GetFiles(@"\\10.0.0.0\c$\Database\", "*.dbf__");
                foreach (string f in RenamedDbfFiles)
                {
                    File.Delete(f);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    private void RenameAndMoveFiles()
    {
        if (File.Exists(@"\\10.0.0.0\c$\Database\" + "file.dbf"))
        {

            File.Move(@"\\10.0.0.0\c$\Database\ + "file.dbf", @"\\10.0.0.0\c$\Database\" + "file.dbf__");
        }
        else if (!File.Exists(@"\\10.0.0.0\c$\Database\" + "*.dbf"))
        {
            File.Move(@"\\10.0.0.0\c$\Database\tempFolder\" + "_file.dbf", @"\\10.0.0.0\c$\Database\" + "file.dbf");
        }
    }
    private void OnElapsedTime(object source, ElapsedEventArgs e)
    {
        CopyFiles();
    }
    protected override void OnStop()
    {
    }
}

}

0 个答案:

没有答案