示例#1
0
        private static int Main(string[] args)
        {
            beVerbose = !string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("VERBOSE"));
            dryrun    = !string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("DRYRUN"));

            if (args.Length < 1)
            {
                HelpMessage();
                return(1);
            }

            path = args[0];
            if (!Directory.Exists(path))
            {
                DisplayMessage("Destination directory does not exist!");
                return(2);
            }
            if (beVerbose)
            {
                Console.WriteLine("Existing files are in: {0}", path);
            }

            string user_profile   = System.Environment.GetEnvironmentVariable("userprofile");
            string path_part2     = "AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets";
            string spotlight_path = Path.Combine(user_profile, path_part2);

            if (args.Length == 2)
            {
                if (args[1].Equals("--only-desktop"))
                {
                    Console.WriteLine("Only processing Desktop images");
                    onlyDesktop = true;
                }
                else
                {
                    if (args[1].Equals("--only-mobile"))
                    {
                        Console.WriteLine("Only processing Mobile images");
                        onlyMobile = true;
                    }
                    else
                    {
                        DisplayMessage(string.Format("Invalid parameter: {0}", args[1]));
                        return(2);
                    }
                }
            }

            Console.WriteLine("Looking for files...");
            var texistingImages = new List <string>(Directory.GetFiles(path));
            var tnewImages      = new List <string>(Directory.GetFiles(spotlight_path));

            var tempExisting = new List <string>(texistingImages);

            foreach (string item in tempExisting)
            {
                if (!item.EndsWith(".spotlight.jpg"))
                {
                    texistingImages.Remove(item);
                }
            }

            if (beVerbose)
            {
                Console.WriteLine("Existing Images: {0}", texistingImages.Count);
                Console.WriteLine("To Filter: {0}", tnewImages.Count);
                Console.WriteLine("Assets dir: {0}", spotlight_path);
            }

            existingImages = FileProcess.GetJpgInfo(texistingImages);
            if (existingImages == null)
            {
                return(1);
            }
            newImages = FileProcess.GetJpgInfo(tnewImages);
            if (newImages == null)
            {
                return(1);
            }

            if (beVerbose)
            {
                Console.WriteLine("After initial filtering, {0} images left to process.", newImages.Count);
            }

            newImages = FileProcess.FilterOutInvalid(newImages, onlyMobile, onlyDesktop);
            if (newImages == null)
            {
                Console.WriteLine("No valid images available");
                return(0);
            }
            if (beVerbose)
            {
                Console.WriteLine("After appropriateness filtering, {0} images remain to process.", newImages.Count);
            }

            List <JpgInfo> filesToCopy = FileProcess.FilesToCopy(existingImages, newImages);

            if (beVerbose)
            {
                Console.WriteLine("Matching Images: {0}", newImages.Count - filesToCopy.Count);
            }

            if (filesToCopy != null && filesToCopy.Count > 0)
            {
                Console.WriteLine("Copying {0} new files...", filesToCopy.Count);
                foreach (JpgInfo item in filesToCopy)
                {
                    string destFileName = Path.Combine(path, item.newFileName);
                    if (!dryrun)
                    {
                        File.Copy(item.filepath, destFileName);
                        File.SetLastWriteTime(destFileName, DateTime.Now);
                    }
                    else
                    {
                        Console.WriteLine("Would've created: {0}", destFileName);
                    }
                }
                Console.WriteLine("Done.");
            }
            else
            {
                Console.WriteLine("No New Files");
            }

            return(0);
        }
        private void doCheck()
        {
            if (!Directory.Exists(dirToSaveTo))
            {
                if (startup)
                {
                    showNotification("Save Directory Does not exist");
                    return;
                }
                if (MessageBox.Show("The Save To directory does not exist, create it?", "Spotlight Image Saver", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        Directory.CreateDirectory(dirToSaveTo);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Could not create save directory.");
                        return;
                    }
                }
                else
                {
                    return;
                }
            }

            string user_profile   = System.Environment.GetEnvironmentVariable("userprofile");
            string path_part2     = "AppData\\Local\\Packages\\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\\LocalState\\Assets";
            string spotlight_path = Path.Combine(user_profile, path_part2);

            var texistingImages = new List <string>(Directory.GetFiles(dirToSaveTo));

            if (texistingImages == null)
            {
                return;
            }
            var tnewImages = new List <string>(Directory.GetFiles(spotlight_path));

            if (tnewImages == null)
            {
                return;
            }

            var tempExisting = new List <string>(texistingImages);

            if (tempExisting == null)
            {
                return;
            }
            foreach (string item in tempExisting)
            {
                if (!item.EndsWith(".spotlight.jpg"))
                {
                    texistingImages.Remove(item);
                }
            }

            var existingImages = FileProcess.GetJpgInfo(texistingImages);

            if (existingImages == null)
            {
                return;
            }
            var newImages = FileProcess.GetJpgInfo(tnewImages);

            if (newImages == null)
            {
                return;
            }

            bool landscapeOnly = false;
            bool portraitOnly  = false;

            if (typeToSave.Equals("Landscape Only"))
            {
                landscapeOnly = true;
            }
            if (typeToSave.Equals("Portrait Only"))
            {
                portraitOnly = true;
            }

            newImages = FileProcess.FilterOutInvalid(newImages, portraitOnly, landscapeOnly);
            if (newImages == null)
            {
                return;
            }
            List <JpgInfo> filesToCopy = FileProcess.FilesToCopy(existingImages, newImages);

            if (filesToCopy != null && filesToCopy.Count > 0)
            {
                foreach (JpgInfo item in filesToCopy)
                {
                    string destFileName = Path.Combine(dirToSaveTo, item.newFileName);
                    File.Copy(item.filepath, destFileName);
                    File.SetLastWriteTime(destFileName, DateTime.Now);
                }
                showNotification(string.Format("You have {0} new backgrounds.", filesToCopy.Count));
            }
        }