Link to home
Start Free TrialLog in
Avatar of jmkotman
jmkotmanFlag for United States of America

asked on

Open File Dialog Box to Open Text File

Below i have attached code, i would like to make it so that when that method is called it brings up a dialog box to open a file rather then hard coding the file name in there.  how would i do that?
FileStream UserRead = new FileStream("A1 UserTransFile a-z.txt", FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
                StreamReader ReadUser = new StreamReader(UserRead);
 
                Transactions = 0;
                data = "";
 
                data = ReadUser.ReadLine();//read in line of data
 
                while (data != null)
                {
                    if (data == "P")//if process key is P, go to print
                    {
                        NodeCount = 0;
                        WriteToLog.WriteLine("P:");//Write transaction P to log
                        Print(rootPtr);//call print
                        WriteToLog.WriteLine();
                        WriteToLog.WriteLine();
                        Transactions++;
                    }
                    else
                    {
                        MethodCall = "Q";
                        Query();
                        Transactions++;
                    }
 
                    data = ReadUser.ReadLine();//read in next line of data
                }
 
                //write transaction to log file
                WriteToLog.WriteLine("*Process BST done: {0} transactions done", Transactions.ToString("d2"));
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

string selecetedFile = "";
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.InitialDirectory = @"C:\\";
            openDialog.Filter = "Text Files|*.txt";
            DialogResult dlgResult = openDialog.ShowDialog();
            if (dlgResult == DialogResult.OK)
            {
                selecetedFile = openDialog.FileName;
            }
            else
            {
                MessageBox.Show("You didnt select a file");
                return;
            }
 
            FileStream UserRead = new FileStream(selecetedFile, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
            //....

Open in new window

Sory Jim, I didnt see your post. :(