Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to open a file in the application using openFileDialog

How to open a file in the application using openFileDialog

Scheduled Pinned Locked Moved C#
helptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Walaza
    wrote on last edited by
    #1

    :doh: Can someone please help me to open a file in the application using openFileDialog. I have a button "Open File" > which then opens an openFileDialog window, I then select the file and click Open, but it does not want to open. I do get the path and everything, but it does not want to open. Maybe I am using the wrong code. Here is a snippet of my code: //Opens the file and populates the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { ofdExcelDoc.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*"; if (ofdExcelDoc.ShowDialog() == DialogResult.OK) { ofdExcelDoc.OpenFile(); try { // Attempt to open the file and output its contents to a text box Stream stream; if ((stream = ofdExcelDoc.OpenFile()) != null) { StreamReader reader = new StreamReader(stream); //dgFileInfo.DataSource = reader.ReadToEnd(); txtOutput.Text = reader.ReadToEnd(); stream.Close(); } } catch (Exception ex) { MessageBox.Show("There was an Error: " + ex.Message); } } } } Mvelo Walaza Developer Telkom SA

    B W X 3 Replies Last reply
    0
    • W Walaza

      :doh: Can someone please help me to open a file in the application using openFileDialog. I have a button "Open File" > which then opens an openFileDialog window, I then select the file and click Open, but it does not want to open. I do get the path and everything, but it does not want to open. Maybe I am using the wrong code. Here is a snippet of my code: //Opens the file and populates the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { ofdExcelDoc.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*"; if (ofdExcelDoc.ShowDialog() == DialogResult.OK) { ofdExcelDoc.OpenFile(); try { // Attempt to open the file and output its contents to a text box Stream stream; if ((stream = ofdExcelDoc.OpenFile()) != null) { StreamReader reader = new StreamReader(stream); //dgFileInfo.DataSource = reader.ReadToEnd(); txtOutput.Text = reader.ReadToEnd(); stream.Close(); } } catch (Exception ex) { MessageBox.Show("There was an Error: " + ex.Message); } } } } Mvelo Walaza Developer Telkom SA

      B Offline
      B Offline
      buchstaben
      wrote on last edited by
      #2

      remove the first occurance of ofdEcvelDoc.OpenFile();

      1 Reply Last reply
      0
      • W Walaza

        :doh: Can someone please help me to open a file in the application using openFileDialog. I have a button "Open File" > which then opens an openFileDialog window, I then select the file and click Open, but it does not want to open. I do get the path and everything, but it does not want to open. Maybe I am using the wrong code. Here is a snippet of my code: //Opens the file and populates the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { ofdExcelDoc.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*"; if (ofdExcelDoc.ShowDialog() == DialogResult.OK) { ofdExcelDoc.OpenFile(); try { // Attempt to open the file and output its contents to a text box Stream stream; if ((stream = ofdExcelDoc.OpenFile()) != null) { StreamReader reader = new StreamReader(stream); //dgFileInfo.DataSource = reader.ReadToEnd(); txtOutput.Text = reader.ReadToEnd(); stream.Close(); } } catch (Exception ex) { MessageBox.Show("There was an Error: " + ex.Message); } } } } Mvelo Walaza Developer Telkom SA

        W Offline
        W Offline
        Walaza
        wrote on last edited by
        #3

        I removed that line but it is still not opening (the file/doc does not get opened)

        Mvelo Walaza Developer Telkom SA

        1 Reply Last reply
        0
        • W Walaza

          :doh: Can someone please help me to open a file in the application using openFileDialog. I have a button "Open File" > which then opens an openFileDialog window, I then select the file and click Open, but it does not want to open. I do get the path and everything, but it does not want to open. Maybe I am using the wrong code. Here is a snippet of my code: //Opens the file and populates the datagrid private void btnOpenFile_Click(object sender, EventArgs e) { ofdExcelDoc.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*"; if (ofdExcelDoc.ShowDialog() == DialogResult.OK) { ofdExcelDoc.OpenFile(); try { // Attempt to open the file and output its contents to a text box Stream stream; if ((stream = ofdExcelDoc.OpenFile()) != null) { StreamReader reader = new StreamReader(stream); //dgFileInfo.DataSource = reader.ReadToEnd(); txtOutput.Text = reader.ReadToEnd(); stream.Close(); } } catch (Exception ex) { MessageBox.Show("There was an Error: " + ex.Message); } } } } Mvelo Walaza Developer Telkom SA

          X Offline
          X Offline
          Xmen Real
          wrote on last edited by
          #4

          replace

          ofdExcelDoc.OpenFile();

          with

          System.Diagnostics.Process.Start(ofdExcelDoc.FileName);

          i hope it will work

          TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can

          W 1 Reply Last reply
          0
          • X Xmen Real

            replace

            ofdExcelDoc.OpenFile();

            with

            System.Diagnostics.Process.Start(ofdExcelDoc.FileName);

            i hope it will work

            TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can

            W Offline
            W Offline
            Walaza
            wrote on last edited by
            #5

            it worked, thanx

            Mvelo Walaza Developer Telkom SA

            X 1 Reply Last reply
            0
            • W Walaza

              it worked, thanx

              Mvelo Walaza Developer Telkom SA

              X Offline
              X Offline
              Xmen Real
              wrote on last edited by
              #6

              glad to know :)

              TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87??6?N8?BcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKSQXUFYK[M`UKs*$GwU#(QDXBER@CBN% Rs0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i?TV.C\y -------------------------------------------------------- 128 bit encrypted signature, crack if you can

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups