| | |
Finding directory of VB project dynamically
![]() |
•
•
Join Date: Jun 2007
Posts: 49
Reputation:
Solved Threads: 2
If someone could tell me if there is a function or method, or some way of finding the directory that the VB project loads to when it is installed on other computers that would be great. What I want it for is so that I can save and restore a database dynamically.
Currently I am saving it to a directory that I know where it is (C:/Program Files/PortGen2.0) Which allows me to save it, but not restore it because I don't know where to load the file if something were to happen to the old one.
Any help is appreciated, some technical info is: VB 2005 Express, Access 2003 Database, and the code I am using right now for restoreDB()
Currently I am saving it to a directory that I know where it is (C:/Program Files/PortGen2.0) Which allows me to save it, but not restore it because I don't know where to load the file if something were to happen to the old one.
Any help is appreciated, some technical info is: VB 2005 Express, Access 2003 Database, and the code I am using right now for restoreDB()
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Public Sub restoreDB() Dim backupName As String Dim portfolioDBName As String = portfolioDBPath + "portfolioDB.mdb" Dim fdlg As OpenFileDialog = New OpenFileDialog() fdlg.InitialDirectory = portfolioDBPath + "\backup" If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then backupName = fdlg.FileName If MessageBox.Show("Restoring the database will erase any changes you have made since your last backup. Are you sure you want to do this?", _ "Confirm Delete", _ MessageBoxButtons.OKCancel, _ MessageBoxIcon.Question, _ MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then 'Restore the database from a backup copy. FileCopy(backupName, portfolioDBName) MsgBox("Database Restoration Successful") End If End If End Sub
Mr.Wobbles~
•
•
Join Date: Jun 2007
Posts: 49
Reputation:
Solved Threads: 2
That works when I debug. However when I publish it and try to work on it as an application then i get an error message - it can't find the file. I looked and figured out it was in a different folder than the application is apparently stored in. Don't know why. So if there is a way to get the path of a the database file that would be awesome.
Mr.Wobbles~
•
•
Join Date: Jun 2007
Posts: 49
Reputation:
Solved Threads: 2
The DB is installed to an alternate folder than the application directory for example:
Directory
Folder 1 = What app.path returns
Folder 2 = Where the database is at
All of the folders repeat some sort of information, but none of them have all of the information, so this one will have the database and maybe the icon or something, and another will have the icon and the forms in it, there is one folder for each of the interops that I used (word, excel and access) and a manifest folder. If none of this helps at all I can get you the real names of the folders tomorrow when I am at work again. Thanks for your help, I appreciate it.
Directory
Folder 1 = What app.path returns
Folder 2 = Where the database is at
All of the folders repeat some sort of information, but none of them have all of the information, so this one will have the database and maybe the icon or something, and another will have the icon and the forms in it, there is one folder for each of the interops that I used (word, excel and access) and a manifest folder. If none of this helps at all I can get you the real names of the folders tomorrow when I am at work again. Thanks for your help, I appreciate it.
Mr.Wobbles~
•
•
•
•
All of the folders repeat some sort of information, but none of them have all of the information, so this one will have the database and maybe the icon or something, and another will have the icon and the forms in it, there is one folder for each of the interops that I used (word, excel and
But what I recommend for dynamically finding the appropriate directory is to either store the information about the data's path in an older INI file (See the Platform SDK for Read/WritePrivateProfile) or to use the registry.
But I would declare a public variable in one of your modules to use as a reference in the program.
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Public mpProgPath as String
Retrieve the value on loading:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
mpProgPath = GetSetting("My Program", "StartUpValues", "AppPath", cstr(App.path))
And then, you can use that as a basis for your other directories where you have information stored:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
strDB_PATH = mpProgPath & "\db" strImage_Path = mpProgPath & "\Images"
Last edited by hkdani; Dec 5th, 2007 at 7:53 pm.
•
•
Join Date: Dec 2007
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
If someone could tell me if there is a function or method, or some way of finding the directory that the VB project loads to when it is installed on other computers that would be great. What I want it for is so that I can save and restore a database dynamically.
Currently I am saving it to a directory that I know where it is (C:/Program Files/PortGen2.0) Which allows me to save it, but not restore it because I don't know where to load the file if something were to happen to the old one.
Any help is appreciated, some technical info is: VB 2005 Express, Access 2003 Database, and the code I am using right now for restoreDB()
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Public Sub restoreDB() Dim backupName As String Dim portfolioDBName As String = portfolioDBPath + "portfolioDB.mdb" Dim fdlg As OpenFileDialog = New OpenFileDialog() fdlg.InitialDirectory = portfolioDBPath + "\backup" If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then backupName = fdlg.FileName If MessageBox.Show("Restoring the database will erase any changes you have made since your last backup. Are you sure you want to do this?", _ "Confirm Delete", _ MessageBoxButtons.OKCancel, _ MessageBoxIcon.Question, _ MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then 'Restore the database from a backup copy. FileCopy(backupName, portfolioDBName) MsgBox("Database Restoration Successful") End If End If End Sub
Try to do this for to find the directory .If you find the directory dynamically then pass the address at the run time .
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Dim objFSO, objFolder, objShell, objTextFile, objFile Dim strDirectory, strFile, strText strDirectory = "d:\gb nair" strFile = "\Summer.txt" strText = "Book Another Holiday" ' Create the File System Object Set objFSO = CreateObject("Scripting.FileSystemObject") ' Check that the strDirectory folder exists If objFSO.FolderExists(strDirectory) Then Set objFolder = objFSO.GetFolder(strDirectory) Else Set objFolder = objFSO.CreateFolder(strDirectory) WScript.echo "Just created " & strDirectory End If If objFSO.FileExists(strDirectory & strFile) Then Set objFolder = objFSO.GetFolder(strDirectory) Else Set objFile = objFSO.CreateTextFile(strDirectory & strFile) WScript.echo "Just created " & strDirectory & strFile End If Set objFile = Nothing Set objFolder = Nothing ' OpenTextFile Method needs a Const value ' ForAppending = 8 ForReading = 1, ForWriting = 2 Const ForAppending = 8 Set objTextFile = objFSO.OpenTextFile _ (strDirectory & strFile, ForAppending, True) ' Writes strText every time you run this VBScript objTextFile.WriteLine (strText) objTextFile.Close ' Bonus or cosmetic section to launch explorer to check file If Err.Number = vbEmpty Then Set objShell = CreateObject("WScript.Shell") objShell.Run ("Explorer" & " " & strDirectory & "\") Else: WScript.echo "VBScript Error: " & Err.Number End If WScript.Quit
Last edited by cscgal; Dec 6th, 2007 at 2:32 pm. Reason: Added code tags
•
•
Join Date: Jun 2007
Posts: 49
Reputation:
Solved Threads: 2
hkdani - the only potential problem I can see with that is my database is in a different folder than what app.path return, the folders that each are in are in the same location, also I can't really go up a directory and specify the folder name because every time I install them then the folder names change - don't know why. I am going to try this anyway, in case I am reading it incorrectly.
gireeshb22 - Not exactly sure about that - maybe I am not understanding but from what I can tell if the file doesn't exist in the specified directory it is created - only problem with that is it is a database and it has to have tables, etc and if I do create it there then I will also have to figure out how to attach it to the program from wherever i create it at. If I am wrong please let me know because some of the functions I am only guessing at what they do.
gireeshb22 - Not exactly sure about that - maybe I am not understanding but from what I can tell if the file doesn't exist in the specified directory it is created - only problem with that is it is a database and it has to have tables, etc and if I do create it there then I will also have to figure out how to attach it to the program from wherever i create it at. If I am wrong please let me know because some of the functions I am only guessing at what they do.
Mr.Wobbles~
•
•
Join Date: Jun 2007
Posts: 37
Reputation:
Solved Threads: 2
Ok, from your post it looks like the Application Folder is in the same folder as the DB Folder.
You can use string manipulation to get the parent folder of your App.Path (use a Do.. ..While Loop and set the string length = to string length until last character = "\" - I think there's a property in FSO that will do this as well, could be GetParentFolder or GetParentDIR or similar)
Once you have the Parent Folder you can use a For.. ..Next Loop to look for your DB:
You can use string manipulation to get the parent folder of your App.Path (use a Do.. ..While Loop and set the string length = to string length until last character = "\" - I think there's a property in FSO that will do this as well, could be GetParentFolder or GetParentDIR or similar)
Once you have the Parent Folder you can use a For.. ..Next Loop to look for your DB:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub cmdFiles_Click() MsgBox GetDBLocation End Sub Private Function GetDBLocation() As String Dim strDBLocation As String Dim fso As New FileSystemObject Dim fldrs As Folder Dim fldr As Folder Dim fle As File Set fldrs = fso.GetFolder("C:\Program Files") For Each fldr In fldrs.SubFolders For Each fle In fldr.Files If fle.ShortName = "YOUR DB NAME" Then strDBLocation = fle.Name Goto ExitHere End If Next fle Next fldr ExitHere: Set fldrs = Nothing Set fso = Nothing If Len(strDBLocation) > 0 Then GetDBLocation = strDBLocation Else GetDBLocation = "Oops! Can't find the DB" End If End Function
Q - P = A
![]() |
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: computer clock
- Next Thread: Control or Object (Other than MDI Form) In vb 6.0 which can hold a Form
| Thread Tools | Search this Thread |
* 6 429 2007 access activex add age application basic beginner birth bmp calculator cd cells.find click client code college component connection connectionproblemusingvb6usingoledb copy creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit error excel excelmacro file filename form hardware header iamthwee image inboxinvb internetfiledownload keypress label listbox listview liveperson login looping machine microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading record refresh remotesqlserverdatabase report save search sendbyte sites sort sql sql2008 sqlserver subroutine tags textbox time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web window windows





