none
Copy only certain files to another folder RRS feed

  • Question

  • Hello everyone,

    I was using Visual Studio C# windows form, How to copy only 3 text files to another folder? 

    According to my coding, after I clicked a button.. all 10 text files in folder A were copied to folder B. But I want 3 text files in folder B. sorry for my bad english grammar :') Below is my coding

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            string FROM_DIR = "C:/Users/N/Desktop/A/";
            string filename = "text" + "*.*";
            string TO_DIR = "C:/Users/N/Desktop/B/";

            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                DirectoryInfo diCopyForm = new DirectoryInfo(FROM_DIR);
                FileInfo[] fiDiskfiles = diCopyForm.GetFiles(filename);
                foreach (FileInfo curFile in fiDiskfiles)
                {
                    try
                    {
               
                        File.Copy(curFile.FullName, TO_DIR + curFile.Name);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

            }
        }
    }

    Thursday, April 11, 2019 2:56 PM

Answers

  • Check this:

       FileInfo[] fiDiskfiles = diCopyForm.GetFiles( "*.txt" );

       foreach( FileInfo curFile in fiDiskfiles.Take(3) )

       . . .

    Thursday, April 11, 2019 4:58 PM

All replies