C# Windows服务定时操作FTP从A服务器读取文件上传到B服务器

本文介绍如何使用C#创建一个Windows服务,该服务通过Timer组件实现定期检查,从服务器A的指定文件夹中读取文件,并通过FTP协议上传到服务器B。项目需求是确保文件夹及其内容能够自动迁移。
摘要由CSDN通过智能技术生成

说明

项目中的一个需求是定期将服务器A中的文件夹及所有文件转存到B服务器中。
解决思路:在A服务器中注册一个Windows服务,通过Timer来控制定期执行,读取A服务器本地文件夹及文件通过FTP的方式上传到B服务器对应的文件夹中。

以下代码是做了一个服务注册工具
效果如下图:
在这里插入图片描述
实现代码如下:

using System;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace SyncAPP
{
   
    public partial class FrmMain : Form
    {
   
        public FrmMain()
        {
   
            InitializeComponent();
        }
        string serviceFilePath = $"{Application.StartupPath}\\SyncService.exe";
        string serviceName = "SyncMainService";

        private void FrmMain_Load(object sender, EventArgs e)
        {
   

        }

        /// <summary>
        /// 判断服务是否存在
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <returns></returns>
        private bool IsServiceExisted(string serviceName)
        {
   
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
   
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
   
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 安装服务
        /// </summary>
        /// <param name="serviceFilePath"></param>
        private void InstallService(string serviceFilePath)
        {
   
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
   
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
                MessageBox.Show("安装成功!", "安装服务", MessageBoxButtons.OK);
            }
        }

        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="serviceFilePath"></param>
        private void UninstallService(string serviceFilePath)
        {
   
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
   
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                installer.Uninstall(null);
                MessageBox.Show("卸载成功!", "卸载服务", MessageBoxButtons.OK);
            }
        }


        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="serviceName"></param>
        private void ServiceStart(string serviceName)
        {
   
            using (ServiceController control = new ServiceController(serviceName))
            {
   
                if (control.Status == ServiceControllerStatus.Stopped)
                {
   
                    control.Start();
                    MessageBox.Show("启动成功!", "启动服务", MessageBoxButtons.OK);
                }
            }
        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="serviceName"></param>
        private void ServiceStop(string serviceName)
        {
   
            using (ServiceController control = new ServiceController(serviceName))
            {
   
                if (control.Status == ServiceControllerStatus.Running)
                {
   
                    control.Stop();
                    MessageBox.Show("停止成功!", "停止服务", MessageBoxButtons.OK);
                }
            }
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
   
            //if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
        }

        private void btnSyncStart_Click(object sender, EventArgs e)
        {
   
            if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
        }

        private void btnSyncClose_Click(object sender, EventArgs e)
        {
   
            if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
        }

        private void btn_init_Click(object sender, EventArgs e)
        {
   
            if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
            this.InstallService(serviceFilePath);
        }

        private void btn_uninstall_Click(object sender, EventArgs e)
        {
   
            if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceFilePath);
        }
    }
}

Windows服务中做逻辑代码:

using Library.Interface;
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值