OpenFileDialog and SaveFileDialog [modified]
-
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:
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 -
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, 2008You 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 anEnvironment.GetFolderPath()
on the fill path and filename.
-Spacix All your skynet questions[^] belong to solved
-
hi all, In my application have 1 database which is point to path
"C:\"
. When I use theOpenFileDialog.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
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 theInitialDirectory
). I don't know if that is what you mean.Matthew Butler
-
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 theInitialDirectory
). I don't know if that is what you mean.Matthew Butler
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 -
hi all, In my application have 1 database which is point to path
"C:\"
. When I use theOpenFileDialog.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
-
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?
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 -
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 -
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 -
Hi Dave, Thanks for fast reply.I will try it :) (I have been solve the problem by using matthew's suggestion)
-
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.
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