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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
AbarajConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
RiteshShahCommented:
BTW, code provided by Abaraj belongs to VB syntax not C#..... I guess you need it in C# only.
0
 
leonov_alexConnect With a Mentor Commented:
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
 
AshokConnect With a Mentor Commented:
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
 
AshokConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.