用一个简单的复选框复选框替换数百个文件
本文关键字:复选框 百个 替换 文件 简单 一个 | 更新日期: 2023-09-27 18:29:59
有没有一种简单或更紧凑的方法可以用一个复选框(选中/未选中)处理大量文件,我想在代码中放几千行,我可以按年份或类型对它们进行排序:
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
}
}
谢谢!
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();
}
这大致就是您想要的吗?
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);
如果没有,请告诉我。