c#使用filesystemwatcher自动移动文件 [英] c# moving files automatically with filesystemwatcher

查看:74
本文介绍了c#使用filesystemwatcher自动移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,



当在定义的文件夹中创建的新jpg文件到另一个文件夹时,是否可以自动移动文件?我想我可以使用filesystemwatcher但我做不到。



Greetings,

is it possible to move file automatically when a new jpg files created in a defined folder to a different folder? i think i can use filesystemwatcher but i couldn't do it.

fileSystemWatcher1.EnableRaisingEvents = true;
         string dst = @"c:\deneme1";
         string src = @"c:\deneme2";

         if (Directory.GetFiles(src).Length == 0)
         {
             string[] move = Directory.GetFiles(src, "*.jpg");
             foreach (string file in move)
             {
                 string file = Path.GetFileName(file);
                 string dstfile = Path.Combine(dst, file);
                 File.Move(file, dstfile);


             }
         }
         else
         {
             string[] move2 = Directory.GetFiles(src, "*.jpg");
             foreach (string file in move2)
             {
                 string file2 = Path.GetFileName(file);
                 string dstfile = Path.Combine(dst, file2);
                 File.Move(file, dstfile);
             }

         }







这不能完美运作。当我开始这个代码。它会将每个jpg文件移动到deneme1文件夹,但是当代码工作时,新的jpg文件将出现在deneme2文件夹中,它将不会移动这个新文件。我怎么能移动这些新文件?




this is not working perfectly. when i start this code. it will move every jpg files to "deneme1" folder but when a new jpg file will appear in "deneme2" folder when code is working, it won't move this new file. how can i move these new files too?

推荐答案

看起来你根本没有使用FileSystemWatcher功能,或者你没有显示所有代码。我无法看到您正在使用代码中的事件。



此示例应该为您完成工作。 MSDN有更广泛的例子: FileSystemWatcher Class [ ^ ]

Looks like you are not using the FileSystemWatcher functionality at all, or you don't show all your code. I can't see that you are using the events in your code.

This example should do the job for you. MSDN has more extensive examples: FileSystemWatcher Class[^]
FileSystemWatcher fileSystemWatcher1 = new FileSystemWatcher();
fileSystemWatcher1.NotifyFilter = NotifyFilters.FileName;
fileSystemWatcher1.Filter = "*.jpg";
fileSystemWatcher1.Path = @"c:\temp\deneme2";
fileSystemWatcher1.Created += fileSystemWatcher1_Created;
fileSystemWatcher1.EnableRaisingEvents = true;

void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
    if (e.ChangeType == WatcherChangeTypes.Created)
    {
        string dest = Path.Combine(@"C:\temp\deneme1", e.Name);
        if (File.Exists(dest))
        {
            File.Delete(e.FullPath);    // For example
        }
        else
        {
            File.Move(e.FullPath, dest);
        }
    }
}


这看起来对我很有用



this looks work great for me

bool xxx;
            fileSystemWatcher1.EnableRaisingEvents = true;
            string dst = @"c:\deneme1";
            string src = @"c:\deneme2";
            Find:
            string[] move = Directory.GetFiles(src, "*.jpg",SearchOption.TopDirectoryOnly);
            if (move.Length != 0)
            {
                xxx = true;
                foreach (string file in move)
                {
                    string a = Path.GetFullPath(file);
                    string files = Path.GetFileName(file);
                    string dstfile = Path.Combine(dst, files);
                    File.Move(a, dstfile);

                }

            }

            else
            {
                xxx = false;
                Console.WriteLine("Empty");
                System.Threading.Thread.Sleep(5000);

            }
            goto Find;


这篇关于c#使用filesystemwatcher自动移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆