Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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);
       }
   }

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.
Posted
Updated 3-Mar-12 20:53pm
v2
Comments
André Kraak 4-Mar-12 2:54am    
Edited question:
"Treat my content as plain text..." option disabled.

1 solution

Hi,

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

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


C#
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");
             }  
}     


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
 
Share this answer
 
v3
Comments
shan1395 4-Mar-12 23:08pm    
Thanks a lot Bjorn Ranft.
El_Codero 5-Mar-12 1:12am    
Glad it works work you. Best Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900