Archive for Agosto 7th, 2008

Files Copy

CodeKeep C# Feed Agosto 7th, 2008

Description: Copy files using Microsoft.VisualBasic.dll

Link: http://www.codekeep.net/snippets/4222772d-724d-460c-b821-aea6372f9bda.aspx

        private int CopyFiles(string dirSource, string dirBackup, string dirTarget)
        {
            int iRet = 0;
            clsConfig.CheckTarget(dirBackup);
            clsConfig.CheckTarget(dirTarget);
            try
            {
                foreach (string foundFile in Microsoft.VisualBasic.FileIO.FileSystem.GetFiles(dirSource, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly))
                {
                    if (foundFile.Substring(foundFile.Length - 4) == ".xml") listBox1.Items.Add(foundFile);
                }
                iRet = listBox1.Items.Count;
                pbFiles.Maximum = iRet;
                try
                {
                    for (int i = 0; i < iRet; i++ )
                    {
                        iReg = i + 1;
                        lblTotal.Text = "Files: " + iReg.ToString() + " of " + iRet.ToString();
                        fileFullName = listBox1.Items[i].ToString();
                        FileInfo fi = new FileInfo(fileFullName);
                        fileName = fi.Name;
                        fi.CopyTo(dirBackup + @"\" + fileName, true);
                        fi.CopyTo(dirTarget + @"\" + fileName,true);
                        fi.Delete();
                        fi = null;
                        pbFiles.Value = iReg;
                        Application.DoEvents();
                    }
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                    MessageBox.Show(ex.Message + " - " + fileName);
                }
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
           
            return iRet;
        }