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. folderBrowserDialog and Saving file

folderBrowserDialog and Saving file

Scheduled Pinned Locked Moved C#
helpalgorithmsquestion
5 Posts 3 Posters 1 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.
  • M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #1

    Hi Guys. I have just spent about 5 hours searching the web trying to find a solution to my problem. I am busy with a project where I am writing data to an Excel file. This works fine. I then want to save the Excel file to a pre-selected directory which the user select using the folderBrowserDialog. Now, I need to know how do I then save my file to the directry selected. I am displaying the Selected path in a txtbox. I only want to save the file to this directory. follderBrowserDialog code:

        private void btSelectDirToSave\_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                txtboxSelectExcelDir.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    

    File save code:

                string namefile;
                namefile = txtboxSelectExcelDir.Text;
    
                objExcel.ActiveWorkbook.SaveAs(namefile + "\_Nom022.xls",Excel.XlFileFormat.xlWorkbookNormal,Type.Missing,Type.Missing,Type.Missing,
                    Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
    

    Any Help would be greatly appreciated as the file is currently not being saved to the selected directory but the Directory name is appended to the filename and saved somewhere else. :( :sigh: :(

    Excellence is doing ordinary things extraordinarily well.

    T S 2 Replies Last reply
    0
    • M MumbleB

      Hi Guys. I have just spent about 5 hours searching the web trying to find a solution to my problem. I am busy with a project where I am writing data to an Excel file. This works fine. I then want to save the Excel file to a pre-selected directory which the user select using the folderBrowserDialog. Now, I need to know how do I then save my file to the directry selected. I am displaying the Selected path in a txtbox. I only want to save the file to this directory. follderBrowserDialog code:

          private void btSelectDirToSave\_Click(object sender, EventArgs e)
          {
              if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
              {
                  txtboxSelectExcelDir.Text = folderBrowserDialog1.SelectedPath;
              }
          }
      

      File save code:

                  string namefile;
                  namefile = txtboxSelectExcelDir.Text;
      
                  objExcel.ActiveWorkbook.SaveAs(namefile + "\_Nom022.xls",Excel.XlFileFormat.xlWorkbookNormal,Type.Missing,Type.Missing,Type.Missing,
                      Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
      

      Any Help would be greatly appreciated as the file is currently not being saved to the selected directory but the Directory name is appended to the filename and saved somewhere else. :( :sigh: :(

      Excellence is doing ordinary things extraordinarily well.

      T Offline
      T Offline
      That Asian Guy
      wrote on last edited by
      #2

      use this method i find it easier

      if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { txtboxSelectExcelDir.Text = folderBrowserDialog1.FileName; }

      // Then saving it as a .txt is what i do... :)
      // Get the info seperated by tabs
      String info = String.Empty;
      using (StreamWriter sw = File.CreateText(folderBrowserDialog1.FileName)
      {
      sw.Write(//whatever info here);

      Make sure the info is seperated by tabs, to indicate different cells for example,

      col1, row1\tcol2, row1\r\ncol1, row2\tcol2, row2

      M 1 Reply Last reply
      0
      • T That Asian Guy

        use this method i find it easier

        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { txtboxSelectExcelDir.Text = folderBrowserDialog1.FileName; }

        // Then saving it as a .txt is what i do... :)
        // Get the info seperated by tabs
        String info = String.Empty;
        using (StreamWriter sw = File.CreateText(folderBrowserDialog1.FileName)
        {
        sw.Write(//whatever info here);

        Make sure the info is seperated by tabs, to indicate different cells for example,

        col1, row1\tcol2, row1\r\ncol1, row2\tcol2, row2

        M Offline
        M Offline
        MumbleB
        wrote on last edited by
        #3

        This means that I would have to change my whole code process. I just need to know how to pass the Directory path to the save function. CHanging the coding I have done is not much of an option.

        Excellence is doing ordinary things extraordinarily well.

        1 Reply Last reply
        0
        • M MumbleB

          Hi Guys. I have just spent about 5 hours searching the web trying to find a solution to my problem. I am busy with a project where I am writing data to an Excel file. This works fine. I then want to save the Excel file to a pre-selected directory which the user select using the folderBrowserDialog. Now, I need to know how do I then save my file to the directry selected. I am displaying the Selected path in a txtbox. I only want to save the file to this directory. follderBrowserDialog code:

              private void btSelectDirToSave\_Click(object sender, EventArgs e)
              {
                  if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                  {
                      txtboxSelectExcelDir.Text = folderBrowserDialog1.SelectedPath;
                  }
              }
          

          File save code:

                      string namefile;
                      namefile = txtboxSelectExcelDir.Text;
          
                      objExcel.ActiveWorkbook.SaveAs(namefile + "\_Nom022.xls",Excel.XlFileFormat.xlWorkbookNormal,Type.Missing,Type.Missing,Type.Missing,
                          Type.Missing,Excel.XlSaveAsAccessMode.xlNoChange,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
          

          Any Help would be greatly appreciated as the file is currently not being saved to the selected directory but the Directory name is appended to the filename and saved somewhere else. :( :sigh: :(

          Excellence is doing ordinary things extraordinarily well.

          S Offline
          S Offline
          Shree
          wrote on last edited by
          #4

          objExcel.ActiveWorkbook.SaveAs(Path.Combine(txtboxSelectExcelDir.Text, namefile + "_Nom022.xls"),...

          M 1 Reply Last reply
          0
          • S Shree

            objExcel.ActiveWorkbook.SaveAs(Path.Combine(txtboxSelectExcelDir.Text, namefile + "_Nom022.xls"),...

            M Offline
            M Offline
            MumbleB
            wrote on last edited by
            #5

            It works 100%. Thanks for that Shree

            Excellence is doing ordinary things extraordinarily well.

            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