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. OpenFileDialog and SaveFileDialog [modified]

OpenFileDialog and SaveFileDialog [modified]

Scheduled Pinned Locked Moved C#
helpquestiondatabasetutorial
15 Posts 6 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.
  • C cocoonwls

    hi spacix, Basically,what i am doing is read excel file to dataset(using oledbconnection) and update this dataset into my database. My original database path is C:\, but when i use OpenFileDialog to get the excel file's path then the database path will be change. So, i will get error when update dataset into database because the .mdb was not found. Any idea?:confused:

    D Offline
    D Offline
    Dave Kreskowiak
    wrote on last edited by
    #6

    This is problably because you're not using fully qualified paths in your code or your using CurrentDirectory in it. You should ALWAYS build fully qualified paths to files in your code to avoid this very problem. Also, using CurrentDirectory to build a path name is not a good idea because all of the File dialog's change what the current directory is and, therefore, can inadventantly change your file paths if they are not built properly.

    A guide to posting questions on CodeProject[^]
    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
         2006, 2007, 2008

    S 1 Reply Last reply
    0
    • D Dave Kreskowiak

      This is problably because you're not using fully qualified paths in your code or your using CurrentDirectory in it. You should ALWAYS build fully qualified paths to files in your code to avoid this very problem. Also, using CurrentDirectory to build a path name is not a good idea because all of the File dialog's change what the current directory is and, therefore, can inadventantly change your file paths if they are not built properly.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      S Offline
      S Offline
      Spacix One
      wrote on last edited by
      #7

      You beat me to it :( It looks like your are using what Microsoft calls the "working directory" which is the last directory your program accessed and can even be changed when a .lnk file is made to your application. If you just do something like

      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.LoadXML("<rootnode><subnode /></rootnode>");
      xmlDoc.Save("myXMLfile.xml");

      it'll spit the XML file out where the last "working directory" is set. You can read where your working at with the Directory.GetCurrentDirectory() method. When you open something say in the same folder as your application. Use the string property "StartUpPath" "System.Windows.Forms.Application" class or hook the current process and get the image path and call an Environment.GetFolderPath() on the fill path and filename.


      -Spacix All your skynet questions[^] belong to solved

      1 Reply Last reply
      0
      • C cocoonwls

        hi all, In my application have 1 database which is point to path "C:\". When I use the OpenFileDialog.FileName to get the other file's path(example , "C:\Documents and Settings\) , the database's path will be change to the file's path. Problem:

        Original Database path: "C:\"
        After use of OpenFileDialog,Database path change to: "C:\Documents and Settings\

        I would like to know how can i maintain or get back to the original path even after use of OpenFileDialog? Can anyone help on this?:confused: thanks in advance cocoon

        modified on Friday, April 18, 2008 3:16 PM

        M Offline
        M Offline
        Matthew Butler 0
        wrote on last edited by
        #8

        If it helps... OpenFileDialog and SaveFileDialog have a RestoreDirectory property... if true, it restores the chosen directory when the dialog closes. (The 'restored' directory is the InitialDirectory). I don't know if that is what you mean.

        Matthew Butler

        D 1 Reply Last reply
        0
        • M Matthew Butler 0

          If it helps... OpenFileDialog and SaveFileDialog have a RestoreDirectory property... if true, it restores the chosen directory when the dialog closes. (The 'restored' directory is the InitialDirectory). I don't know if that is what you mean.

          Matthew Butler

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #9

          I wonder, while navigating a FileDialog on the UI thread, if the current directory changes on a background thread BEFORE the dialog closes and returns from RestoreDirectory?? Hmmm, something I'll have to test when I get some free time.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          C 1 Reply Last reply
          0
          • C cocoonwls

            hi all, In my application have 1 database which is point to path "C:\". When I use the OpenFileDialog.FileName to get the other file's path(example , "C:\Documents and Settings\) , the database's path will be change to the file's path. Problem:

            Original Database path: "C:\"
            After use of OpenFileDialog,Database path change to: "C:\Documents and Settings\

            I would like to know how can i maintain or get back to the original path even after use of OpenFileDialog? Can anyone help on this?:confused: thanks in advance cocoon

            modified on Friday, April 18, 2008 3:16 PM

            C Offline
            C Offline
            cocoonwls
            wrote on last edited by
            #10

            Hi Dave, what is your mean by "fully qualify path"?Can you please give an example? Spanix, thanks for you reply,i will try it :) Matthew, isn't if i use the restoredirectory after OpenFileDialog, the path will get back to original path?

            D 1 Reply Last reply
            0
            • C cocoonwls

              Hi Dave, what is your mean by "fully qualify path"?Can you please give an example? Spanix, thanks for you reply,i will try it :) Matthew, isn't if i use the restoredirectory after OpenFileDialog, the path will get back to original path?

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #11

              Full drive and path specification:

              C:\Program Files\CompanyName\AppName\SomeFolder\SomeFile.txt

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              C 1 Reply Last reply
              0
              • D Dave Kreskowiak

                I wonder, while navigating a FileDialog on the UI thread, if the current directory changes on a background thread BEFORE the dialog closes and returns from RestoreDirectory?? Hmmm, something I'll have to test when I get some free time.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                C Offline
                C Offline
                cocoonwls
                wrote on last edited by
                #12

                Hi Matthew, Its work great to me.Now after i openfiledialog, it will restore the original path for me.Thanks :laugh: regard cocoon

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Full drive and path specification:

                  C:\Program Files\CompanyName\AppName\SomeFolder\SomeFile.txt

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  C Offline
                  C Offline
                  cocoonwls
                  wrote on last edited by
                  #13

                  Hi Dave, Thanks for fast reply.I will try it :) (I have been solve the problem by using matthew's suggestion)

                  D 1 Reply Last reply
                  0
                  • C cocoonwls

                    Hi Dave, Thanks for fast reply.I will try it :) (I have been solve the problem by using matthew's suggestion)

                    D Offline
                    D Offline
                    darkelv
                    wrote on last edited by
                    #14

                    It's still good and future proof to use full path for the database in case next time other people use OpenFileDialog and didn't set the restore flag.

                    S 1 Reply Last reply
                    0
                    • D darkelv

                      It's still good and future proof to use full path for the database in case next time other people use OpenFileDialog and didn't set the restore flag.

                      S Offline
                      S Offline
                      Spacix One
                      wrote on last edited by
                      #15

                      I seem to recall there is even a method that can convert a relative path such as "..\afolder\blah.txt" into a fully qualified path. It's in System.IO I think, I'll edit my post when I remember the method name. Edit: here it is: http://msdn2.microsoft.com/en-us/library/system.io.path.getfullpath(VS.80).aspx[^]


                      -Spacix All your skynet questions[^] belong to solved

                      modified on Saturday, April 19, 2008 2:43 PM

                      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