0
        private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog.FileName = "ok";
        StreamReader SR = new StreamReader(@"C:\Users\Murat\Pictures\New folder\DE.TXT");
        string satir;
        int sayac=0;
        while((satir = SR.ReadLine()) != null)
            {
            //listBox1.Items.Add(satir);
            richTextBox1.AppendText(satir + "\n");
            sayac++;
            }
        SR.Close();

OpenFileDialog.FileName is could not :( help please.

I want to enter a file path while reading data from a file.

  • 3
    Possible duplicate of [Reading From a Text File in C#](https://stackoverflow.com/questions/7980456/reading-from-a-text-file-in-c-sharp) – waka Sep 14 '17 at 11:52
  • Don't you want OpenFileDialogFilename = "DE.TXT"? You can set openFileDialog1.InitialDirectory. – jdweng Sep 14 '17 at 12:10
  • yes but with openFileDialog –  Sep 14 '17 at 12:14

1 Answers1

1

Here's an example showing how to use the OpenFileDialog:

    private void button1_Click(object sender, EventArgs e)
    {
        if (OpenFileDialog.ShowDialog() == DialogResult.OK)
        {             
            richTextBox1.Clear();
            using (StreamReader SR = new StreamReader(OpenFileDialog.FileName))
            {
                string satir;
                while ((satir = SR.ReadLine()) != null)
                {
                    richTextBox1.AppendText(satir + "\n");
                }
            }
            int sayac = richTextBox1.Lines.Count();
        }   
    }

Note that you could also just do:

    private void button1_Click(object sender, EventArgs e)
    {
        if (OpenFileDialog.ShowDialog() == DialogResult.OK)
        {             
            richTextBox1.Lines = File.ReadAllLines(OpenFileDialog.FileName);
            int sayac = richTextBox1.Lines.Count();
        }   
    }
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • openFileDialog does not have such a property either. –  Sep 14 '17 at 12:13
  • openFileDialog errormessage: an object reference is required for the non-static field, method, or property commonDialog.showDialog –  Sep 14 '17 at 12:23
  • Does not have **what** property? Your second error message indicates that you do not have an OpenFileDialog on your form. Did you add one to your form? Usually it would be called "openFileDialog1". – Idle_Mind Sep 14 '17 at 22:28