Browse button code in C#

Is there any easy to implement way to have Browse button in in C# Windows application?
TheCommunicatorAsked:
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.

AbarajCommented:
think this code is help u

try this ok

Dim str As String

Dim openFileDialog1 As New OpenFileDialog()

openFileDialog1.Filter = "txt files (*.txt)|*.txt"

(this is only for textfiles if u want all files u have to write beside of .txt ||All files (*.*)|*.*

this is for all files ok

u have to use what ever u want

ok all the best)

openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True

If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

msgbox("opened")

Else

MsgBox("not opend")
End If
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
RiteshShahCommented:
I would suggest you to follow first link (C-SharpCorner.com) from my three link, it has detailed good example over other two links
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

RiteshShahCommented:
BTW, code provided by Abaraj belongs to VB syntax not C#..... I guess you need it in C# only.
0
leonov_alexCommented:
Two event handlers for browse buttons, for file and directory in C#:

private void Browse_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog()
    {
        Filter = "Text files (*.txt)|*.txt",
        Multiselect = false,
        RestoreDirectory = true
    };

    if (ofd.ShowDialog() == DialogResult.OK)
        MessageBox.Show(ofd.FileName);
    else
        MessageBox.Show("Not selected!");
}

private void BrowseFolder_Click(object sender, EventArgs e)
{
    FolderBrowserDialog fbd = new FolderBrowserDialog()
    {
        RootFolder = Environment.SpecialFolder.DesktopDirectory,
        Description = "Select folder"
    };

    if (fbd.ShowDialog() == DialogResult.OK)
        MessageBox.Show(fbd.SelectedPath);
    else
        MessageBox.Show("Not selected!");
}
0
AshokSr. Software EngineerCommented:
Drop openFileDialog component on the form and.....

    public void btnBrowse_Click(object sender, EventArgs e)
    {
      openFileDialog1.Title = "Select an Image";
      openFileDialog1.Filter = "Icons|*.ico|Bitmap|*.bmp|JPEG|*.jpg|GIF Images|*.gif|PNG Images|*.png";
      openFileDialog1.FileName = "";
      openFileDialog1.RestoreDirectory = true

      if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
      {
        string Chosen_File = openFileDialog1.FileName;
        // your code here.....

      }
    }

HTH
Ashok
0
AshokSr. Software EngineerCommented:
Drop openFileDialog component on the form and.....

    public void btnBrowse_Click(object sender, EventArgs e)
    {
      openFileDialog1.Title = "Select an Image";
      openFileDialog1.Filter = "Icons|*.ico|Bitmap|*.bmp|JPEG|*.jpg|GIF Images|*.gif|PNG Images|*.png";
      openFileDialog1.FileName = "";
      openFileDialog1.RestoreDirectory = true;

      if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
      {
        string Chosen_File = openFileDialog1.FileName;
        // your code here.....

      }
    }

HTH
Ashok
0
TheCommunicatorAuthor Commented:
Hello guys,
                           I am so sorry for not getting back to you on time. Well, the links provided by ritesh shah, specialy the first one was really good and the code worked. But as i could not respond and distribute the points on the time there were another answers. I have looked through the answers and they are also very appealing solutions.
I am not sure how I am going to distribute points but I am feelign obliged to give every one some point who have  helped me through the solution.
i think ritesh wouldn't mind me doing this. :).
 
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
.NET Programming

From novice to tech pro — start learning today.