Example #1
0
        public static bool WalkDirectory(
            string environment,
            int depth,
            string directory,
            bool prependFilename,
            out List <string> subdirectories,
            out List <string> files,
            out long bytes,
            bool recursive)
        {
            subdirectories = new List <string>();
            files          = new List <string>();
            bytes          = 0;

            try
            {
                subdirectories = Common.GetSubdirectoryList(directory, false);
                files          = Common.GetFileList(environment, directory, prependFilename);

                if (files != null && files.Count > 0)
                {
                    foreach (String currFile in files)
                    {
                        FileInfo fi = new FileInfo(currFile);
                        bytes += fi.Length;
                    }
                }

                List <string> queueSubdirectories = new List <string>();
                List <string> queueFiles          = new List <string>();
                long          queueBytes          = 0;

                if (recursive)
                {
                    if (subdirectories == null || subdirectories.Count < 1)
                    {
                        return(true);
                    }
                    depth += 2;

                    foreach (string curr in subdirectories)
                    {
                        List <string> childSubdirectories = new List <string>();
                        List <string> childFiles          = new List <string>();
                        long          childBytes          = 0;

                        WalkDirectory(
                            environment,
                            depth,
                            curr,
                            prependFilename,
                            out childSubdirectories,
                            out childFiles,
                            out childBytes,
                            true);

                        if (childSubdirectories != null)
                        {
                            foreach (string childSubdir in childSubdirectories)
                            {
                                queueSubdirectories.Add(childSubdir);
                            }
                        }

                        if (childFiles != null)
                        {
                            foreach (string childFile in childFiles)
                            {
                                queueFiles.Add(childFile);
                            }
                        }

                        queueBytes += childBytes;
                    }
                }

                if (queueSubdirectories != null)
                {
                    foreach (string queueSubdir in queueSubdirectories)
                    {
                        subdirectories.Add(queueSubdir);
                    }
                }

                if (queueFiles != null)
                {
                    foreach (string queueFile in queueFiles)
                    {
                        files.Add(queueFile);
                    }
                }

                bytes += queueBytes;
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }