我正在根据我的.ini文件删除一些名称。我的 .ini文件中列出了一些名称。每当我的应用程序处理输入文件时...它会从.ini文件中查找某些关键字,如果在我的输入文件中找到关键字,则会删除整行。
但对我来说,当.ini文件在我的文本文件&&中找到密钥搜索时,正在发生的事情。如果我的密钥搜索在我的第二列中,则仅删除该行。我想在我的文本文件中搜索关键字以查找我的第一列。我怎么能添加这个??
代码段:
public void do_name()
{
string old;
string iniPath = Application.StartupPath + "\\list.ini";
bool isDeleteSectionFound = false;
List<string> deleteCodeList = new List<string>();
using (StreamReader sr = File.OpenText(iniPath))
{
while ((old = sr.ReadLine()) != null)
{
if (old.Trim().Equals("[DELETE]"))
{
isDeleteSectionFound = true;
}
if (isDeleteSectionFound && !old.Trim().Equals("[DELETE]"))
{
deleteCodeList.Add(old.Trim());
}
}
}
StringBuilder sb = new StringBuilder();
using (StreamReader sr = File.OpenText(textBox1.Text))
{
while ((old = sr.ReadLine()) != null)
{
var st = old.Trim().Split(new char[] { '\t' });
if (st.Length > 1)
{
var tempCode = st[1].Substring(1, st[1].Length - 2);
if (!deleteCodeList.Contains(tempCode))
{
sb.AppendLine(old);
}
}
else if (st.Length == 1)
{
//old = "\n";
sb.AppendLine(old);
}
}
}
File.WriteAllText(textBox1.Text, sb.ToString());
}
我的输入文本文件:您可以看到,由于某些原因,未搜索第一列值。我不知道..
Designator MAX PN Footprint Center-X(mm) Center-Y(mm)
"C10" "100-0009" "1206 - CAPACITOR" "122.492" "69.469"
"C100" "100-0009" "1206 - CAPACITOR" "264.211" "12.814"
"C102" "100-0009" "1206 - CAPACITOR" "251.346" "11.201"
I would like to add C10, C100 and all first column elements to my .ini file
我的.ini文件看起来像
[DELETE]
100-2333
233-3233
C10
答案 0 :(得分:1)
看起来你只看着“1”栏。由于数组是零索引,这实际上是你的第二列。这是一个如何获取第1列和第2列的示例,替换while循环中的代码:
var st = old.Trim().Split(new char[] { '\t' });
bool hasDeleteMatch = false;
//loop through all the columns starting at 0 and up to 1 (inclusive)
for (int col = 0; col < st.Length && col<=1; col++)
{
if (st[col].Length>2)
{
var tempCode = st[col].Substring(1, st[col].Length - 2);
if (!deleteCodeList.Contains(tempCode))
{
hasDeleteMatch = true; //we found a match, don't append to the new file
break;
}
}
}
if (!hasDeleteMatch) sb.AppendLine(old);
如果您想将此扩展到更多列,请将col<=1
更改为更大的数字,或将其删除以搜索所有列。
答案 1 :(得分:0)
你正在使用st [1]而不是st [0] .. st是此行中split函数的结果数组:
var st = old.Trim().Split(new char[] { '\t' });
因为数组索引是零,你应该使用0而不是1
编辑 - 基于MikeH回答:
var st = old.Trim().Split(new char[] { '\t' });
bool hasDeleteMatch = false;
//loop through all the columns starting at 0 and up to 1 (inclusive)
var columns = new int[0, 2]; // the columns you want to remove
for (int i = 0; i < columns.Length; i++)
{
int col = columns[i];
if (st[col].Length>2)
{
var tempCode = st[col].Substring(1, st[col].Length - 2);
if (!deleteCodeList.Contains(tempCode))
{
hasDeleteMatch = true; //we found a match, don't append to the new file
break;
}
}
}
if (!hasDeleteMatch) sb.AppendLine(old);
答案 2 :(得分:0)
当我为我的搜索行删除它做了两个不同的功能时它工作..但我认为这是太长的代码..如果有任何机构有想法结合这个...请发布答案..我会接受它!
功能:在我的文本文件的第一列中进行键搜索
public void do_name1()
{
string old;
string iniPath = Application.StartupPath + "\\list.ini";
bool isDeleteSectionFound = false;
List<string> deleteCodeList = new List<string>();
using (StreamReader sr = File.OpenText(iniPath))
{
while ((old = sr.ReadLine()) != null)
{
if (old.Trim().Equals("[DELETE]"))
{
isDeleteSectionFound = true;
}
if (isDeleteSectionFound && !old.Trim().Equals("[DELETE]"))
{
deleteCodeList.Add(old.Trim());
}
}
}
StringBuilder sb = new StringBuilder();
using (StreamReader sr = File.OpenText(textBox1.Text))
{
while ((old = sr.ReadLine()) != null)
{
var st = old.Trim().Split(new char[] { '\t' });
if (st.Length > 1)
{
var tempCode1 = st[0].Substring(1, st[0].Length - 2);
if (!deleteCodeList.Contains(tempCode1))
{
sb.AppendLine(old);
}
}
else if (st.Length == 1)
{
//old = "\n";
sb.AppendLine(old);
}
}
}
File.WriteAllText(textBox1.Text, sb.ToString());
}
功能:从第二列删除搜索到的文本。
public void do_name1()
{
string old;
string iniPath = Application.StartupPath + "\\list.ini";
bool isDeleteSectionFound = false;
List<string> deleteCodeList = new List<string>();
using (StreamReader sr = File.OpenText(iniPath))
{
while ((old = sr.ReadLine()) != null)
{
if (old.Trim().Equals("[DELETE]"))
{
isDeleteSectionFound = true;
}
if (isDeleteSectionFound && !old.Trim().Equals("[DELETE]"))
{
deleteCodeList.Add(old.Trim());
}
}
}
StringBuilder sb = new StringBuilder();
using (StreamReader sr = File.OpenText(textBox1.Text))
{
while ((old = sr.ReadLine()) != null)
{
var st = old.Trim().Split(new char[] { '\t' });
if (st.Length > 1)
{
var tempCode = st[1].Substring(1, st[1].Length - 2);
if (!deleteCodeList.Contains(tempCode))
{
sb.AppendLine(old);
}
}
else if (st.Length == 1)
{
//old = "\n";
sb.AppendLine(old);
}
}
}
File.WriteAllText(textBox1.Text, sb.ToString());
}