Reputation: 2146
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();
bool b;
foreach (string file in filteredfiles)
{
if(b=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". So it failed to copy in to local.
Upvotes: 0
Views: 2480
Reputation: 160962
You can use DirectoryInfo
to filter down to any file that contains the string "test":
private FileInfo[] GetFileList(string pattern)
{
var di = new DirectoryInfo(@"\\testserver\dev");
return di.GetFiles(pattern);
}
and then:
foreach (var file in GetFileList("*test*"))
{
getfiles.copytolocal(file.FullName);
}
Upvotes: 1