doubts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • senthiltr86
    New Member
    • Oct 2008
    • 9

    doubts

    hai i am senthil.i am using visual studio 2005 .i want to open the file open window when i click the browse button.can you please tell me how it is happen?.thank you.
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    You don't specify what language you are working with, but the OpenFileDialog class should do the job for you. Here are some examples:

    In VB.NET:

    Code:
    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim myStream As Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()
    
        openFileDialog1.InitialDirectory = "c:\"
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True
    
        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            Try
                myStream = openFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then
                    ' Insert code to read the stream here.
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub
    In C#:

    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
    
        openFileDialog1.InitialDirectory = "c:\\" ;
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
        openFileDialog1.FilterIndex = 2 ;
        openFileDialog1.RestoreDirectory = true ;
    
        if(openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        // Insert code to read the stream here.
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }

    Comment

    • senthiltr86
      New Member
      • Oct 2008
      • 9

      #3
      thank you very much.

      Comment

      Working...