-3

このコードは正常に機能しますが、私の問題は、ESSDを含むファイルを読み取る必要があり、このファイルの特定の行に名前が含まれている場合はリストボックス1に追加し、その行に名前が含まれていない場合は追加しないことです。リストボックス1。

ありがとう。

private void button1_Click(object sender, EventArgs e)
    {
        DialogResult result = this.openFileDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            string[] Nomarchivo = this.openFileDialog1.FileNames;
            string NomDirec = Path.GetDirectoryName(Nomarchivo[0]);
            for (int a = 0; a <= Nomarchivo.Length - 1; a++)
            {
                string NomDirGral = Nomarchivo[a].Remove(Nomarchivo[a].Length - 7, 7);
                string NomGral = NomDirGral.Replace(NomDirec, " ");
                NomGral = NomGral.Remove(0, 2);
                foreach (string f in Directory.GetFiles(NomDirec, NomGral + "*"))
                    this.listBox1.Items.Add(f);
                foreach (string h in Directory.GetFiles(NomDirec, "resume*"))
                    this.listBox1.Items.Add(h);
                foreach (string t in Directory.GetFiles(NomDirec, "ESSD1*"))
                    this.listBox1.Items.Add(t);
            }
            string[] list1 = new string[listBox1.Items.Count];
            for (int b = 0; b <= listBox1.Items.Count - 1; b++)
            {
                list1[b] = listBox1.Items[b].ToString();
            }
            string[] list2 = list1.Distinct().ToArray();
            foreach (string g in list2)
                this.listBox2.Items.Add(g);

            Class1 arr1 = new Class1();
            arr1.array(listBox2);
        }
        else { Close(); }
    }
4

3 に答える 3

0

問題は、ESSD ファイルにアクセスして 6 行目を読みたいということです。この行に Resume ファイルに保存されている名前が含まれている場合は、ESSD ファイルを listbox1 に送信し、そうでない場合は ESSD を listbox1 に追加しません。

そう:

  1. 履歴書ファイルの読み取り/解析
  2. 履歴書リストを並べ替えます。
  3. すべての ESSD ファイルのリストを作成する
  4. 各ファイルから「6行目」を読み取って、上記のリストを繰り返します
  5. 「6行目」の名前で履歴書ファイルを検索します
  6. 見つかったら追加

それを上記のコードに追加して、何が起こるかを確認してください。それがどうなるかを見て、おそらくあなたの質問を修正してください。

于 2012-09-14T19:19:24.123 に答える
0

次の 2 つの方法があります。

public static string ReadSpecificLine1( string path , int lineNumber )
{
  int cnt = 0 ;
  string desiredLine = File.ReadAllLines( path ).FirstOrDefault( x => (++cnt == lineNumber) ) ;
  return desiredLine ;
}

public static string ReadSpecificLine2( string path , int lineNumber )
{
  string desiredLine = null ;
  using ( FileStream   stream = new FileStream( path , FileMode.Open , FileAccess.Read , FileShare.Read ) )
  using ( StreamReader reader = new StreamReader( stream ) )
  {
    int    i           = 0    ;
    string line        = null ;
    while ( null != (line=reader.ReadLine()) && ++i < lineNumber )
    {
    }
    if ( i == lineNumber && line != null )
    {
       desiredLine = line ;
    }
    return desiredLine ;
  }
}

あなたのファイルが

  • ファイルの Unicode エンコード方式で単一の固定サイズ Unicode コード単位を構成する文字のみを含み、かつ

  • 固定長の行 (レコード) があります

次にseek()、UTF-16 エンコーディングを使用したこの例のように、問題の行に直接移動できるため、余分な I/O 操作を節約できます。

public static string ReadSpecificLineFromUTF16EncodedFile( string path , int lineNumber )
{
  string    desiredLine                    = null ;

  using ( FileStream   stream = new FileStream( path , FileMode.Open , FileAccess.Read , FileShare.Read ) )
  using ( StreamReader reader = new StreamReader( stream , Encoding.Unicode ) )
  {
    Encoding  UTF_16                         = Encoding.Unicode ;
    const int RECORD_LENGTH_IN_CHARS         = 80 ;
    const int FIXED_CODE_UNIT_SIZE_IN_OCTETS = sizeof(char) ; // size of a UTF-16 code unit (char) in octets
    const int RECORD_LENGTH_IN_OCTETS        = RECORD_LENGTH_IN_CHARS * FIXED_CODE_UNIT_SIZE_IN_OCTETS ;

    long offset = (lineNumber-1)*RECORD_LENGTH_IN_OCTETS ;

    if ( offset <  0 ) throw new ArgumentOutOfRangeException("lineNumber") ;
    if ( offset <= stream.Length )
    {
      stream.Seek( offset , SeekOrigin.Begin ) ;
      desiredLine = reader.ReadLine() ;
    }
  }
  return desiredLine ;
}

これは、上記の 2 つの条件が true の場合にのみ機能することに注意してください。

于 2012-09-14T19:12:28.247 に答える
0

.NET 4 は File.ReadLines(fileName) と呼ばれる優れた関数を提供します

この関数を使用すると、上記のコードを次のように変更できます。

private void button1_Click(object sender, EventArgs e) 
{ 
    DialogResult result = this.openFileDialog1.ShowDialog(); 
    if (result == DialogResult.OK) 
    { 
        string[] Nomarchivo = this.openFileDialog1.FileNames; 
        string NomDirec = Path.GetDirectoryName(Nomarchivo[0]); 
        for (int a = 0; a <= Nomarchivo.Length - 1; a++) 
        { 

            //the name we are looking for.
            List<string> namesWeAreLookingFor = new List<string>();

            string NomDirGral = Nomarchivo[a].Remove(Nomarchivo[a].Length - 7, 7); 
            string NomGral = NomDirGral.Replace(NomDirec, " "); 
            NomGral = NomGral.Remove(0, 2); 
            foreach (string f in Directory.GetFiles(NomDirec, NomGral + "*")) 
                this.listBox1.Items.Add(f); 
            foreach (string h in Directory.GetFiles(NomDirec, "resume*")) 
            {
                this.listBox1.Items.Add(h); 

                var nameLines = File.ReadLines(NomDirec + @"\" + h);
                foreach (var item in nameLines)
                {
                    //do whatever you need to get a name in this file here
                    //...

                    //Assuming there is one name per line, add the name to the list
                    namesWeAreLookingFor.Add(item);
                }
            }
            foreach (string t in Directory.GetFiles(NomDirec, "ESSD1*")) 
            {
                this.listBox1.Items.Add(t); 
                //try to access the file so we can read line 6 using new .NET 4 method 
                var lines = File.ReadLines(NomDirec + @"\" + t);

                //see if we even have 6 lines
                if (lines.Count() < 6)
                    continue;

                String line6 = lines.ElementAt(5);

                //loop through the names we pulled
                foreach (var item in namesWeAreLookingFor)
                {
                    //see if line 6  containes that name.
                    if (line6.Contains(item))
                    {
                        //if it exists, then add it to the list and exit the loop.
                        this.listBox1.Items.Add(t);
                        break;
                    }
                }
            }
        } 
        string[] list1 = new string[listBox1.Items.Count]; 
        for (int b = 0; b <= listBox1.Items.Count - 1; b++) 
        { 
            list1[b] = listBox1.Items[b].ToString(); 
        } 
        string[] list2 = list1.Distinct().ToArray(); 
        foreach (string g in list2) 
            this.listBox2.Items.Add(g); 

        Class1 arr1 = new Class1(); 
        arr1.array(listBox2); 
    } 
    else { Close(); } 
于 2012-09-14T19:22:35.127 に答える