Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1.  OpenFileDialog loadList = new OpenFileDialog();
  2.  
  3.             /// what files to open
  4.             loadList.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
  5.             loadList.FilterIndex = 1;
  6.             /// multi select, is false.
  7.             loadList.Multiselect = false;
  8.  
  9.  
  10.             // open filedialog
  11.             loadList.ShowDialog();
  12.  
  13.  
  14.  
  15.             /// read lines from the file
  16.             FileStream filereader;
  17.             try
  18.             {
  19.  
  20.                 filereader = new FileStream(loadList.FileName, FileMode.Open);
  21.  
  22.             }
  23.             catch (IOException)
  24.             {
  25.                 MessageBox.Show("Please exit the file before loading it!");
  26.                 return;
  27.             }
  28.  
  29.             byte[] textbuffer = new byte[filereader.Length];
  30.  
  31.             filereader.Read(textbuffer, 0, textbuffer.Length);
  32.            
  33.             for (int i = 0; i < textbuffer.Length; i++)
  34.             {
  35.                 //// 13 + 10 represents end of line ( char val )
  36.                 if (textbuffer[i] == 13)
  37.                 {
  38.                     if (i + 1 < textbuffer.Length && textbuffer[i + 1] == 10)
  39.                     {
  40.                         linesread++;
  41.                     }
  42.  
  43.                 }
  44.  
  45.             }
  46.             /// here you'll actually read the lines
  47.             StringReader read = new StringReader(Encoding.ASCII.GetString(textbuffer));
  48.             for (int i = 0; i <= linesread; i++)
  49.             {
  50.                
  51.                     ListViewItem item = new ListViewItem();
  52.                     item.Text = read.ReadLine();
  53.                     if (item.Text.Length != 0)
  54.                     {
  55.                         listView1.Items.Add(item);
  56.                        
  57.                    
  58.                     }
  59.  
  60.  
  61.             }
  62.            
  63.             read.Close();
  64.             filereader.Dispose();
  65.             filereader.Close();
  66.  
  67.             listView1.Refresh();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement