Create a class level
private
variable, and put the path in there:
private string pathToFile = null;
public void openCVSFile(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.Filter = "CSV files (*.csv)|*.csv";
ofd.FilterIndex = 1;
if(ofd.ShowDialog() == DialogResult.OK)
{
txtAddressCount.Text = ("Address count: "+ ofd.FileName);
pathToFile = ofd.FileName;
}
}
private void btnPrintFile_Click(object sender, EventArgs e)
{
if (pathToFile == null) return;
try
{
streamToPrint = new StreamReader(pathToFile);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
pd.Print();
}
finally
{
streamToPrint.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}