コード例 #1
0
        private static int CopyFileTo(string CopyFromRoot, string CopyToRoot, List <string> FilesToCopy)
        {
            int result = 0; //Return number of files copied.

            LogOperations myLog = new LogOperations();

            int kk = 10001;

            foreach (string file in FilesToCopy)
            {
                kk += 9;

                string DirectoryToCheckExists = Directory.GetParent(CopyToRoot + file).ToString() + "\\";

                if (Directory.Exists(DirectoryToCheckExists))
                {
                    try
                    {
                        File.Copy(CopyFromRoot + file, CopyToRoot + file);
                        myLog.WriteToLogFile("Copied " + CopyFromRoot + file + " To " + CopyToRoot + file);
                        result++;
                    }
                    catch (FileNotFoundException ex)
                    {
                        myLog.WriteToErrorLog(ex.Message + " " + ex.FileName);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        myLog.WriteToErrorLog(ex.Message + " " + ex.Source);
                    }
                }
                else
                {
                    try
                    {
                        Directory.CreateDirectory(Directory.GetParent(CopyToRoot + file).ToString());
                        File.Copy(CopyFromRoot + file, CopyToRoot + file);
                        myLog.WriteToLogFile("Copied " + CopyFromRoot + file + " To " + CopyToRoot + file);
                        result++;
                    }
                    catch (FileNotFoundException ex)
                    {
                        myLog.WriteToErrorLog(ex.Message + " " + ex.FileName);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        myLog.WriteToErrorLog(ex.Message + " " + ex.Source);
                    }
                }
            }

            myLog.WriteLogs();

            return(result);
        }
コード例 #2
0
        public List <string> GetFileNames(string origin)
        {
            List <string>  result            = new List <string>();
            Queue <string> ListOfDirectories = new Queue <string>();

            ListOfDirectories.Enqueue(origin);

            LogOperations ErrorLog = new LogOperations();


            string[] Files;
            string[] SubDirs;

            while (ListOfDirectories.Count > 0)
            {
                string currentDirectory = ListOfDirectories.Dequeue();

                try
                {
                    Files = Directory.GetFiles(currentDirectory);

                    foreach (string file in Files)
                    {
                        result.Add(file.Replace(origin, ""));
                    }

                    SubDirs = Directory.GetDirectories(currentDirectory);

                    foreach (string subDirectory in SubDirs)
                    {
                        ListOfDirectories.Enqueue(subDirectory);
                    }
                }
                catch (DirectoryNotFoundException ex)
                {
                    ErrorLog.WriteToErrorLog("Get File Names: " + ex.Message);
                    result.Add("Error");
                }
                catch (UnauthorizedAccessException ex)
                {
                    ErrorLog.WriteToErrorLog("Get File Names: " + ex.Message);
                    result.Add("Error");
                }
            }

            if (ErrorLog.ErrorMessageCount > 1)
            {
                ErrorLog.WriteLogs();
            }
            else
            {
            }
            return(result);
        }