How to avoid using foreach loop to get the filelist for different reason
<p>Here what am trying to do:</p>

<ol>
<li><p>I have a remote server (e.g:svr01,svr02,svr03). Using <code>GetFileList</code> to read the directory get all the files and match with the file name I have then copy to my local drive.</p></li>
<li><p>If any files matched then am adding them to an XML file also.</p></li>
</ol>

<p>I was trying to do like below</p>

<pre><code> class Program
{
    static void Main(string[] args)
    {
        var getfiles = new fileshare.Program();

        string realname = "*main*";
        string Location = "SVR01";
        bool anymatch = false;
         foreach (var file in getfiles.GetFileList(realname,Location))
        {anymatch=true;}
         if (anymatch == true)
         { baseMeta(); }
        foreach (var file in getfiles.GetFileList(realname,Location))
        {getfiles.copytolocal(file.FullName); }

        }

    private FileInfo[] GetFileList(string pattern,string Location)
    {

        try
        {
            switch (Location)
            {
                case "SVR01":
                    {
                        var di = new DirectoryInfo(@"\\SVR01\Dev");
                        return di.GetFiles(pattern);
                    }
                case "SVR02":
                    {
                        var di = new DirectoryInfo(@"\\SVR02\Dev");
                        return di.GetFiles(pattern);
                    }
                case "SVR03":
                    {
                        var di = new DirectoryInfo(@"\\SVR03\Prod");
                        return di.GetFiles(pattern);
                    }
                default: throw new ArgumentOutOfRangeException();

            }
        }
        catch(Exception ex)
        { Console.Write(ex.ToString());
        return null;
        }

    }
    private void copytolocal(string filename)
    {
        string nameonly = Path.GetFileName(filename);
        File.Copy(filename,Path.Combine(@"c:\",nameonly),true);

    }

    private void baseMeta()
    {

                XmlWriter xmlWrite = XmlWriter.Create(@"c:\basexml");
                xmlWrite.WriteStartElement("job");
                xmlWrite.WriteElementString("Name", "test");
                xmlWrite.WriteElementString("time", DateTime);
                xmlWrite.Close();

    }
}
</code></pre>

<p>but this piece of code worries me because am doing the same process two times, any one please guide me how to avoid this.</p>

<pre><code> foreach (var file in getfiles.GetFileList(realname,Location))
 {
     anymatch=true;}
     if (anymatch == true)
     { 
          baseMeta(); 
     }
     foreach (var file in getfiles.GetFileList(realname,Location))
     {
         getfiles.copytolocal(file.FullName); 
     }
 }
</code></pre>

<p>Even am trying to find out if it match anyfile then i quit the first foreach loop generate the <strong>basemeta()</strong> then goes to next foreach loop to do the rest of the process.</p>
