C# 第一列名称未从我的文本文件中删除

C# 第一列名称未从我的文本文件中删除,c#,string,visual-studio-2010,text,ini,C#,String,Visual Studio 2010,Text,Ini,我正在根据我的.ini文件删除一些名称。我的.ini文件中列出了一些名称。每次我的应用程序处理输入文件时。。它从.ini文件中查找某些关键字,如果在我的输入文件中找到该关键字,它将删除整行 但是对我来说,当.ini文件在我的文本文件中找到一个键搜索时,&如果我的键搜索在我的第二列中,那么只有删除该行。我希望关键字搜索也能在我的文本文件中查找我的第一列。我怎样才能添加这个 代码段: public void do_name() { string old; st

我正在根据我的.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());
    }
我的.ini文件看起来像

 [DELETE]
 100-2333
 233-3233
 C10

您正在使用st[1]而不是st[0]。。st是此行拆分函数的结果数组:

  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);
var st=old.Trim().Split(新字符[]{'\t'});
bool hasDeleteMatch=false;
//循环遍历从0到1(包括0)的所有列
var columns=newint[0,2];//要删除的列
for(int i=0;i2)
{
var tempCode=st[col]。子字符串(1,st[col]。长度-2);
如果(!deleteCodeList.Contains(tempCode))
{
hasDeleteMatch=true;//我们找到了一个匹配项,不附加到新文件
打破
}
}
}
如果(!hasDeleteMatch)某人追加行(旧);

看起来您只看了“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);
var st=old.Trim().Split(新字符[]{'\t'});
bool hasDeleteMatch=false;
//循环遍历从0到1(包括0)的所有列
对于(int col=0;col

如果您想将其扩展到更多列,请更改
col当我为搜索行删除创建了两个不同的函数时,它起到了作用。。但我认为这太长了。。如果有人想把这个结合起来。。请把答案贴出来。。我会接受的

功能:在我的文本文件的第一列中进行键搜索

 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()
{
串旧;
字符串iniPath=Application.StartupPath+“\\list.ini”;
bool isDeleteSectionFound=false;
List deleteCodeList=新列表();
使用(StreamReader sr=File.OpenText(iniPath))
{
而((old=sr.ReadLine())!=null)
{
if(old.Trim()等于(“[DELETE]”)
{
isDeleteSectionFound=true;
}
if(isDeleteSectionFound&!old.Trim()等于(“[DELETE]”)
{
deleteCodeList.Add(old.Trim());
}
}
}
StringBuilder sb=新的StringBuilder();
使用(StreamReader sr=File.OpenText(textBox1.Text))
{
而((old=sr.ReadLine())!=null)
{
var st=old.Trim().Split(新字符[]{'\t'});
如果(标准长度>1)
{
var tempCode1=st[0]。子字符串(1,st[0]。长度-2);
如果(!deleteCodeList.Contains(tempCode1))
{
某人(老);
}
}
否则如果(st.Length==1)
{
//old=“\n”;
某人(老);
}
}
}
File.writealText(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());
    }
public void do_name1()
{
串旧;
字符串iniPath=Application.StartupPath+“\\list.ini”;
bool isDeleteSectionFound=false;
List deleteCodeList=新列表();
使用(StreamReader sr=File.OpenText(iniPath))
{
而((old=sr.ReadLine())!=null)
{
if(old.Trim()等于(“[DELETE]”)
{
isDeleteSectionFound=true;
}
if(isDeleteSectionFound&!old.Trim()等于(“[DELETE]”)
{
deleteCodeList.Add(old.Trim());
}
}
}
StringBuilder sb=新的StringBuilder();
使用(StreamReader sr=File.OpenText(textBox1.Text))
{
而((old=sr.ReadLine())!=null)
{
var st=old.Trim().Split(新字符[]{'\t'});
如果(标准长度>1)
{
var tempCode=st[1]。子字符串(1,st[1]。长度-2);
如果(!deleteCodeList.Contains(tempCode))
{
某人(老);
}
}
否则如果(st.Length==1)
{
//old=“\n”;
某人(老);
}
}
}
File.writealText(textBox1.Text,sb.ToString());
}

获取错误:-
其他信息:startIndex不能大于leng
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());
    }