Beispiel #1
0
        //Method that will check if exceptions are valid
        public void CheckExceptions(string[] exceptions)
        {
            //Instantiate FileExceptions property
            FileExceptions = new List <string>();

            if (Array.Find(exceptions, z => z.StartsWith("*.")) != "")
            {
                WildCardExceptions = new List <string>();
            }

            CopyDirectories = SafeFileEnumerator.EnumerateDirectories(DataSource, "*", SearchOption.TopDirectoryOnly).ToList();

            //Check if there are exceptions
            if (exceptions[0] != "")
            {
                //Iterate through exceptions to see if paths are valid
                for (int i = 0; i < exceptions.Length; i++)
                {
                    try
                    {
                        //Does exception exist
                        if (File.Exists(exceptions[i]) || Directory.Exists(exceptions[i]))
                        {
                            //Get file attributes of exception to determine if it's a file or directory
                            FileAttributes fileAttributes = File.GetAttributes(exceptions[i]);

                            if ((fileAttributes & FileAttributes.Directory) == FileAttributes.Directory)
                            {
                                if (CopyDirectories.Contains(exceptions[i]))
                                {
                                    CopyDirectories.Remove(exceptions[i]);
                                }

                                else
                                {
                                    foreach (string x in SafeFileEnumerator.EnumerateFiles(exceptions[i], "*", SearchOption.AllDirectories))
                                    {
                                        FileExceptions.Add(x);
                                    }
                                }
                            }
                            else
                            {
                                FileExceptions.Add(exceptions[i]);
                            }
                        }
                        else if (exceptions[i].Contains("*."))
                        {
                            WildCardExceptions.Add(exceptions[i].Remove(0, 1));
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.LogWrite(_exceptionError);
                        Log.LogWrite(ex.Message);
                        continue;
                    }
                }
            }
        }
Beispiel #2
0
        //Method to enumerates files then retrieve file info for those files
        private static List <SimpleFileInfo> GetFileData(List <string> exceptions, List <string> copyDirectories, List <string> wildCards)
        {
            //Instantiate objects for use
            List <SimpleFileInfo> files = new List <SimpleFileInfo>();
            FileInfo fileInfo;

            //Used to calculate bin packing
            //const int bytesPerMb = 1048576;

            //Use SafeFileEnumerator to get files then retrieve file info for those files
            for (int i = 0; i < copyDirectories.Count(); i++)
            {
                foreach (string x in SafeFileEnumerator.EnumerateFiles(copyDirectories[i], "*", SearchOption.AllDirectories).Except(exceptions))
                {
                    try
                    {
                        fileInfo = new FileInfo(x);
                        SimpleFileInfo simpleFileInfo = new SimpleFileInfo
                        {
                            Path   = fileInfo.FullName,
                            SizeMb = (double)Math.Ceiling((decimal)(fileInfo.Length)) // / bytesPerMb))
                        };
                        files.Add(simpleFileInfo);
                    }
                    catch (UnauthorizedAccessException ex)
                    {
                        Log.LogWrite(_exceptionError);
                        Log.LogWrite(ex.Message);
                        Log.LogWrite(ex.Source);
                        continue;
                    }
                }
            }

            if (wildCards != null)
            {
                foreach (string x in wildCards)
                {
                    files.RemoveAll(z => z.Path.EndsWith(x));
                }
            }

            //Sort list by descending to aid in packing
            //return files.OrderByDescending(f => f.SizeMb).ToList();
            return(files.ToList());
        }