用一个简单的复选框检查替换数百个文件

Replacing hundreds of files with one simple check box check

是否有一种简单或更紧凑的方法可以通过一个复选框 (checked/unchecked) 对大量文件执行此操作,我想在代码中放入几千行并且我可以按年份或类型对它们进行排序:

private void CheckBox()
{
    try
    {
        switch (checkBox.IsChecked)
        {
            case true:
            {
                const string disable_picture100 = "images/disabled/picture100.png";
                const string picture100 = "images\disabled\picture100.png";
                Records[picture100].ReplaceContents(imagesPath, disable_picture100, content.FileRoot);

                const string disable_picture101 = "images/disabled/picture101.png";
                const string picture101 = "images\disabled\picture101.png";
                Records[picture101].ReplaceContents(imagesPath, disable_picture101, content.FileRoot);

                const string disable_picture102 = "images/disabled/picture102.png";
                const string picture102 = "images\disabled\picture102.png";
                Records[picture102].ReplaceContents(imagesPath, disable_picture102, content.FileRoot);

                UpdateImage();
            }
            break;

            case false:
            {
                const string enable_picture100 = "images/enabled/picture100.png";
                const string picture100 = "images\enabled\picture100.png";
                Records[picture100].ReplaceContents(imagesPath, enable_picture100, content.FileRoot);

                const string enable_picture101 = "images/enabled/picture101.png";
                const string picture101 = "images\enabled\picture101.png";
                Records[picture101].ReplaceContents(imagesPath, enable_picture101, content.FileRoot);

                const string enable_picture102 = "images/enabled/picture102.png";
                const string picture102 = "images\enabled\picture102.png";
                Records[picture102].ReplaceContents(imagesPath, enable_picture102, content.FileRoot);

                UpdateImage();
            }
            break;
        }
    }
    catch (Exception ex)
    {
        //ignored
    }
}

谢谢!

这就是您要查找的内容吗?

        string pictureName;
        string newPictureName;

        List<string> fileNames = new List<string>(); 

        foreach(var name in fileNames)
        {
            if (checkBox.IsChecked)
            {
                pictureName = "images\disabled\" + name + ".png";
                newPictureName = "images/disabled/" + name + ".png";

            }
            else
            {
                pictureName = "images\enabled\" + name + ".png";
                newPictureName = "images/enabled/" + name + ".png";
            }
        }

        Records[pictureName].ReplaceContents(imagesPath, newPictureName, content.FileRoot);

如果没有请告诉我。

List<string> fileNames = new List<string>(); //suppose you have names of files in a list
foreach(var name in fileNames)
{
    if(checkBox.IsChecked)
    {
        Records[name].ReplaceContents
        ("images/disabled/" + name, "images\disabled\" + name, content.FileRoot);
    }
    else
    {
        Records[name].ReplaceContents
        ("images/enabled/" + name, "images\enabled\" + name, content.FileRoot);
    }
}

使用下面的代码您可以指定一个目录(其中字符串显示 "FilePath"。它会获取所有扩展名为 .png

的文件

然后检查复选框是否被选中。 然后遍历枚举器

中的所有文件
        var allPngFilesInGivenDirectory = Directory.EnumerateFiles("FilePath").Where(x => x.ToLower().EndsWith(".png"));
        var fileEnumerable = allPngFilesInGivenDirectory.GetEnumerator();

        string partialPath = checkBox.IsChecked ? "enabled" : "disabled";

        while (fileEnumerable.MoveNext())
        {
            string file = Path.GetFileName(fileEnumerable.Current);

            string disable_picture = "images/" + partialPath + "/" + file;
            string picture = "images\" + partialPath + "\" + file;
            Records[picture].ReplaceContents(imagesPath, disable_picture, content.FileRoot);

            UpdateImage();
        }