• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2406
  • Last Modified:

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

0
jmkotman
Asked:
jmkotman
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now