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

jmkotmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JimBrandleyCommented:
If this is a Windows App, you can do it like this:

string filePath = string.Empty;
string initialPath = "C:\\Temp";

OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = initialPath;
dlg.DefaultExt = "xml";
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
   filePath = dlg.FileName;
}
else
   // No file selected.

Jim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
monarch_ilhanCommented:

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

0
monarch_ilhanCommented:
Sory Jim, I didnt see your post. :(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.