Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7604191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:50:52+00:00 2026-05-30T23:50:52+00:00

Here what am trying to do: I have a remote server (e.g:svr01,svr02,svr03). Using GetFileList

  • 0

Here what am trying to do:

  1. I have a remote server (e.g:svr01,svr02,svr03). Using GetFileList to read the directory get all the files and match with the file name I have then copy to my local drive.

  2. If any files matched then am adding them to an XML file also.

I was trying to do like below

 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();

    }
}

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

 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); 
     }
 }

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:50:53+00:00Added an answer on May 30, 2026 at 11:50 pm

    Using LINQ you should be able to easily change your posted code into:

    var getfiles = new fileshare.Program();
    string realname = "*main*";
    string Location = "SVR01";
    var fileList = getFiles.GetFileList(realname, Location);
    var anymatch = fileList.Any();
    
    if (anymatch) // Or possibly `if (fileList.Any())` if anymatch isn't 
                  // really used anywhere else
        baseMeta();
    
    foreach (var file in getfiles.GetFileList(realname,Location))
        getfiles.copytolocal(file.FullName);
    

    You’ll get the greatest benefit by replacing your GetFileList method with:

    private IEnumerable<FileInfo> GetFileList(string pattern,string Location)
    {
        string directory = string.Empty;
    
        switch (Location)
        {
            case "SVR01":
                directory = @"\\SVR01\Dev";
            break;
    
            case "SVR02":
                directory = @"\\SVR02\Dev";
            break;
    
            case "SVR03":
                directory = @"\\SVR03\Prod");
            break;
    
            default: 
                throw new ArgumentOutOfRangeException();
    
        }
    
        DirectoryInfo di = null;
        try
        {
            di = new DirectoryInfo(directory);
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
            yield break;
        }
    
        foreach(var fi in di.EnumerateFiles(pattern))
            yield return fi;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use uploadify to upload images to a remote server. I've have
How can I create a directory on a remote server using .NET? Both servers
I'm trying to establish an interactive SSH connection to a remote server using PHP
Here's what I'm trying to do : I have an entity Task with a
Here is a problem I am trying to solve: I have an irregular shape.
here's what I'm trying to do : On a page, I would have a
Here is what I am trying to do. I have a block of text
Here's what I'm trying to do. I have an ASP.NET MVC web application, where
I have an interesting problem here I've been trying to solve for the last
Here's what i'm trying to do. I have a project in /var/www/project. I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.