4est
4est

Reputation: 3168

Pass File Directory between button click

How can I pass file directory between two button click method?

  1. I'm selecting csv file and put the CSV name to TextBox

    private void Button_Click(object sender, RoutedEventArgs e)
    {
      OpenFileDialog ofd = new OpenFileDialog();
      ofd.Filter = "CSV|*.csv";
      ofd.ShowDialog();
    
      csvName.Text = ofd.SafeFileName;           
    }
    
  2. Second button should start reading CSV file selected into first step

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        //string path = ;
        StreamReader srd = new StreamReader();
    }
    

Upvotes: 0

Views: 677

Answers (2)

Phanindra
Phanindra

Reputation: 69

csvName.Text = ofd.SafeFileName; SafeFileName doesn't return the full path it just returns the filename, which is not enough for the button(Button_Click_1) the start reading . Instead use ofd.FileName; this give the complete path. Assuming csvName as TextBox.

declare global variable

public string FullFileName{get;set;}

private void Button_Click(object sender, RoutedEventArgs e)
{
 OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "CSV|*.csv";
ofd.ShowDialog();
csvName.Text = ofd.SafeFileName;           

FullFileName=ofd.FileName; }

private void Button_Click_1(object sender, RoutedEventArgs e)
{
using(var reader = new StreamReader(FullFileName))
{
// do your action here
}
}

Upvotes: 1

hitesh sharma
hitesh sharma

Reputation: 69

you can read the text of csvName in the button2_click event.

string path = csvName.Text;

or you can use a variable to save the value of the textbox and use that too.

Upvotes: 0

Related Questions