C# 替换列表视图C中选中的文件上的字符串#
我正在尝试创建一个程序来替换.txt文件中的字符串。 这是诀窍。如果选中,我将替换文件中的字符串, 但当我做另一个检查时,它仍然会替换另一个C# 替换列表视图C中选中的文件上的字符串#,c#,file,listview,checkbox,C#,File,Listview,Checkbox,我正在尝试创建一个程序来替换.txt文件中的字符串。 这是诀窍。如果选中,我将替换文件中的字符串, 但当我做另一个检查时,它仍然会替换另一个 private void BatchReplace() { string sourceFolder = FilePath.Text; string searchWord = Searchbar.Text; DateTime now = DateTime.Now; List&
private void BatchReplace()
{
string sourceFolder = FilePath.Text;
string searchWord = Searchbar.Text;
DateTime now = DateTime.Now;
List<string> allFiles = new List<string>();
AddFileNamesToList(sourceFolder, allFiles);
if (listView1.CheckedItems.Count != 0)
{
foreach (String file in allFiles)
{
for (int x = 0; x <= listView1.CheckedItems.Count - 1; x++)
{
if (file.Contains(listView1.CheckedItems[x].Text))
{
MessageBox.Show("File contains: " + listView1.CheckedItems[x].Text);
try
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to replace \"" + Searchbar.Text + "\" with \"" + Replacebar.Text + "\"?", "WARNING!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
StreamReader reader = new StreamReader(file);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, Searchbar.Text, Replacebar.Text);
StreamWriter writer = new StreamWriter(file);
writer.Write(content); writer.Close();
}
else
{
}
}
catch
{
}
}
}
}
}
}
else
{
MessageBox.Show("Please Check the files you want to rename");
}
}
public static void AddFileNamesToList(string sourceDir, List<string> allFiles)
{
string[] fileEntries = Directory.GetFiles(sourceDir);
try
{
foreach (string fileName in fileEntries)
{
allFiles.Add(fileName);
}
//Recursion
string[] subdirectoryEntries = Directory.GetDirectories(sourceDir);
foreach (string item in subdirectoryEntries)
{
// Avoid "reparse points"
if ((File.GetAttributes(item) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
{
AddFileNamesToList(item, allFiles);
}
}
}
private void BatchReplace()
{
字符串sourceFolder=FilePath.Text;
字符串searchWord=Searchbar.Text;
DateTime now=DateTime.now;
List allFiles=新列表();
AddFileNamesToList(源文件夹,所有文件);
如果(listView1.CheckedItems.Count!=0)
{
foreach(所有文件中的字符串文件)
{
对于(int x=0;x我仍然不清楚您试图做什么,但是为了简化事情,为什么不在使用目录中的文件填充列表视图时,将文件路径(或文件对象)添加到列表视图项的标记属性中
这样,当您循环检查选中的项目时,您可以直接检索文件,而不必同时循环检查两个列表
比如:
private void BatchReplace()
{
string sourceFolder = FilePath.Text;
string searchWord = Searchbar.Text;
AddFileNamesToList(sourceFolder);
if (listView1.CheckedItems.Count == 0)
{
MessageBox.Show("Please Check the files you want to rename");
return;
}
for (int x = 0; x < listView1.CheckedItems.Count; x++)
{
var file = listView1.CheckedItems[x].Tag.ToString()
try
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to replace \"" + Searchbar.Text + "\" with \"" + Replacebar.Text + "\"?", "WARNING!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
StreamReader reader = new StreamReader(file);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, SearchWord, Replacebar.Text);
StreamWriter writer = new StreamWriter(file);
writer.Write(content);
writer.Close();
}
}
catch
{
}
}
}
}
private void BatchReplace()
{
字符串sourceFolder=FilePath.Text;
字符串searchWord=Searchbar.Text;
AddFileNamesToList(sourceFolder);
如果(listView1.CheckedItems.Count==0)
{
Show(“请检查要重命名的文件”);
返回;
}
对于(int x=0;x
很抱歉出现缩进,而且如果它不能像现在这样直接工作,我还没有测试过它。我仍然不知道您要做什么,但是为了简化事情,为什么不在使用目录中的文件填充列表视图时,添加文件路径(或文件对象)到列表视图项的标记属性
这样,当您循环检查选中的项目时,您可以直接检索文件,而不必同时循环检查两个列表
比如:
private void BatchReplace()
{
string sourceFolder = FilePath.Text;
string searchWord = Searchbar.Text;
AddFileNamesToList(sourceFolder);
if (listView1.CheckedItems.Count == 0)
{
MessageBox.Show("Please Check the files you want to rename");
return;
}
for (int x = 0; x < listView1.CheckedItems.Count; x++)
{
var file = listView1.CheckedItems[x].Tag.ToString()
try
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to replace \"" + Searchbar.Text + "\" with \"" + Replacebar.Text + "\"?", "WARNING!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
StreamReader reader = new StreamReader(file);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, SearchWord, Replacebar.Text);
StreamWriter writer = new StreamWriter(file);
writer.Write(content);
writer.Close();
}
}
catch
{
}
}
}
}
private void BatchReplace()
{
字符串sourceFolder=FilePath.Text;
字符串searchWord=Searchbar.Text;
AddFileNamesToList(sourceFolder);
如果(listView1.CheckedItems.Count==0)
{
Show(“请检查要重命名的文件”);
返回;
}
对于(int x=0;x
很抱歉出现缩进,而且如果它不能正常工作,我还没有对此进行测试。你能澄清一下你所说的是什么意思吗?但是当我做另一个检查时,它仍然会替换另一个
?例如,我在listview上有这个,x 123.txt 456.txt x 789.txt 234.txt,那么如果我检查了123和789,它仍然是r将字符串直接替换到234文件中,这是因为您在条件file.Contains(listView1.CheckedItems[x].Text)处获得了匹配项
?我认为一种更简单的方法是将文件存储在字典中,然后循环检查检查项并从字典中检索相关文件。请您澄清一下是什么意思,但当我执行另一项检查时,它仍在替换另一项
?例如,我在listview上有这个例子,x 123.txt 456.txt x 789.txt 234.txt,如果我选中了123和789,它仍然会替换234文件中的字符串,这是因为您在条件file.Contains(listView1.CheckedItems[x].Text)中找到了匹配项
?我认为一种更简单的方法是将文件存储在字典中,然后循环检查检查项并从字典中检索相关文件。