有效的方法来获取文件并将其从远程复制到本地 [英] Efficient way to get files and copy from Remote to local

查看:87
本文介绍了有效的方法来获取文件并将其从远程复制到本地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取远程目录中的文件列表,并仅检查文件名称为"test"的文件,然后将其复制到本地目录中.

只是在这里做了一件简单的事情,但是有人可以让我知道处理这种情况的最佳方法.

I was trying to get the list of files in my remote directory and check only the file has name "test", then copy to my local directory.

Just did a simple thing here, but can someone please let me know the best way to handle this scenario.

class Program
   {
       static void Main(string[] args)
       {
           var getfiles = new fileshare.Program();
           string[] filteredfiles =getfiles.GetFileList();


           foreach (string file in filteredfiles)
           {
               if(file.Contains("test"))
               {
                   getfiles.copytolocal(file);
               }

           }

       }

       private string[] GetFileList()
       {
           string[] filepaths = Directory.GetFiles(@"\\testserver\dev");
           return filepaths;
       }
       private void copytolocal(string filename)
       {
           File.Copy(filename, @"C:\" + filename);
       }
   }


即使我只是在复制文件时卡住,文件名也包含文件名内的整个目录,因此文件名看起来像"\\\\ testserver \\ dev \\ test.txt".
无法复制到本地文件夹.


Even I just stuck up when I was copy the file, the filename contains the whole directory inside the filename so filename look like "\\\\testserver\\dev\\test.txt".
It failed to copy in to local folder.

推荐答案



也许您尝试用这种方法将远程测试文件复制到本地服务器.

Hi,

maybe you try this way to copy your remote testfile to local server.

string[] fileEntries = Directory.GetFiles(@"YOUR_SERVER_DIRECTORY");
           foreach (string fileName in fileEntries)
               ProcessFile(fileName);





public static void ProcessFile(string filename)
{
             FileInfo finfo = new FileInfo(filename);

             //finfo.Name = only the name of the file, finfo.FullName = full path of the file including filename,
             if (finfo.Name == "test")
             {
                 File.Copy(finfo_fir.FullName,@"C:\DESTINATION_PATH");
             }  
}     



如果尝试提高复制文件的性能,则提供的File.Copy()-Method非常健壮.
在某些情况下,一定要注意一些事情,例如在多线程中使用Asynchcronus FileStreams,并注意缓冲区大小并将其调整为RAM,从而提高性能.
这是一个很好的讨论:

> http://stackoverflow.com/questions/1286354/how-to- make-my-application-copy-file-faster [ ^ ]
http://stackoverflow.com/questions/1246899/file- copy-vs-manual-filestream-write-for-copying-file [



If you try to increase the performance of copying a file, the delivered File.Copy()-Method is pretty robust.
In some cases it''s certainly possible to increase the Performance by taking care of a few things like using Asynchcronus FileStreams in Multithreading and taking care of your buffersize and adjusting it to the RAM.
Here''s a very good discussion about it:

http://stackoverflow.com/questions/1286354/how-to-make-my-application-copy-file-faster[^]
http://stackoverflow.com/questions/1246899/file-copy-vs-manual-filestream-write-for-copying-file[^]


Regards


这篇关于有效的方法来获取文件并将其从远程复制到本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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