2013-10-05 54 views
0

我是C#中的新手,我有一个简单的控制台应用程序和方法validateVoters()它采用studentID参数,将其与文本文件进行比较,然后返回相应的布尔值。 但是我希望它删除特定的studentID,如果它存在然后返回true,但没有从文件方法的通用删除,所以我使用了一个成员在这里推荐的方法: 给我一个错误的方法在双星号** :
错误2
'RemoveUnnecessaryLine' 不在当前上下文中存在C的名称:\用户\ Hlogoyatau \文件\的Visual Studio 2010 \项目\ Ijoo \ Ijoo \ Program.cs的28 43 Ijoo从C#中的文本文件中删除一行#

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 

namespace SRCVotingSystem 
{ 

public class Program 
{ 


    public bool validateVoter(String cisNo) 
    { 
     bool found = false; 

     try 
     { 
      string[] ID = System.IO.File.ReadAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt"); 

      foreach (string line in ID) 
      { 
       //compares it against text file contents 
       if (cisNo == line) 
       { 
        string[] allLines= File.ReadAllLines("votersRoll.txt"); 
        string[] newIDs= **RemoveUnnecessaryLine**(allLines); 
        File.WriteAllLines("votersRoll.txt", newIDs); 


        found = true; 
       } 
      } 
     } 

     catch (IOException e) 
     { 
      Console.WriteLine(e.ToString()); 
     } 

     return found; 
    } 


    public static void Main() 
    { 

     Program vv = new Program(); 
     Console.WriteLine(vv.validateVoter("cis11-005")); 
    } 

    } 
} 
+0

因为它实际上并不存在......这是你自己写。 –

+1

组成一个函数名不会神奇地创建一个函数,它可以做你想做的事 –

+0

哦...谢谢虽然:( – maatla

回答

0

试试这个:

public bool validateVoter(String cisNo) 
{ 
    bool found = false; 

    try 
    { 
     string[] ID = System.IO.File.ReadAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt"); 

     for (int i = 0; i < ID.Length; i++) 
     { 
      string line = ID[i]; 
      //compares it against text file contents 
      if (cisNo == line) 
      { 
       //Shift remaining lines up, overwriting current line 
       for (int j = i; j < ID.Length - 1; j++) 
       { 
        ID[j] = ID[j+1]; 
       } 

       //Set last line to empty string 
       ID[ID.Length - 1] = ""; 

       //Write file back to disk 
       System.IO.File.WriteAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt", ID); 

       found = true; 

       //Exit loop after something is found 
       break; 
      } 
     } 
    } 

    catch (IOException e) 
    { 
     Console.WriteLine(e.ToString()); 
    } 

    return found; 
} 

它将读取文件,并且当找到匹配项时,则将剩余的行移动一行。最后一行将被清除,然后文件被写回磁盘。如果你不想有空的最后一行,那么你可以调整数组大小(参见Array.Resize)。

0

尝试使用LINQ

public void validateVoter(String cisNo) 
{ 
    var newIDs = System.IO.File.ReadAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt").Where(l => l != cisNo); 
    File.WriteAllLines(@"C:\Users\Hlogoyatau\Pictures\votersRoll.txt", newIDs); 
} 
2
/* sample data in text.tx 
ID 1 asdfsdaf 
ID 2 asdfdsafasdfsadf 
ID 3 lkjasdfjsdf 
*/ 
private static void Main(string[] args) 
{ 
    var id = 2; 
    var lines = File.ReadAllLines("C:\\temp\\text.txt"); 

    var remaining = lines.Where(x => !x.Contains(id.ToString())).ToArray(); 
    File.WriteAllLines("C:\\temp\\out.txt", remaining); 
}