Finding directory of VB project dynamically

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2007
Posts: 49
Reputation: Mr.Wobbles is an unknown quantity at this point 
Solved Threads: 2
Mr.Wobbles Mr.Wobbles is offline Offline
Light Poster

Finding directory of VB project dynamically

 
0
  #1
Dec 4th, 2007
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)
  1. Public Sub restoreDB()
  2.  
  3. Dim backupName As String
  4. Dim portfolioDBName As String = portfolioDBPath + "portfolioDB.mdb"
  5.  
  6. Dim fdlg As OpenFileDialog = New OpenFileDialog()
  7.  
  8. fdlg.InitialDirectory = portfolioDBPath + "\backup"
  9.  
  10. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  11. backupName = fdlg.FileName
  12.  
  13. 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?", _
  14. "Confirm Delete", _
  15. MessageBoxButtons.OKCancel, _
  16. MessageBoxIcon.Question, _
  17. MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
  18. 'Restore the database from a backup copy.
  19. FileCopy(backupName, portfolioDBName)
  20.  
  21. MsgBox("Database Restoration Successful")
  22. End If
  23. End If
  24.  
  25. End Sub
Mr.Wobbles~
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 42
Reputation: Chris147 is an unknown quantity at this point 
Solved Threads: 2
Chris147 Chris147 is offline Offline
Light Poster

Re: Finding directory of VB project dynamically

 
0
  #2
Dec 4th, 2007
Hi Mr Wobbles (good name!)

For VB6 use: App.Path to return the application dir (eg: Open App.Path & "\MyFile.txt" For Input as #1)

For VB2005 use My.Application.Info.DirectoryPath

HTH,

Chris.
Q - P = A
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 49
Reputation: Mr.Wobbles is an unknown quantity at this point 
Solved Threads: 2
Mr.Wobbles Mr.Wobbles is offline Offline
Light Poster

Re: Finding directory of VB project dynamically

 
0
  #3
Dec 5th, 2007
That worked well, thanks! I don't know why I didn't see that before. (I'm not being sarcastic if it sounds that way... seriously)
Mr.Wobbles~
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 49
Reputation: Mr.Wobbles is an unknown quantity at this point 
Solved Threads: 2
Mr.Wobbles Mr.Wobbles is offline Offline
Light Poster

Re: Finding directory of VB project dynamically

 
0
  #4
Dec 5th, 2007
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~
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 42
Reputation: Chris147 is an unknown quantity at this point 
Solved Threads: 2
Chris147 Chris147 is offline Offline
Light Poster

Re: Finding directory of VB project dynamically

 
0
  #5
Dec 5th, 2007
Ah.

This appears to be an installation issue.

I had 2005 Express for a while, got fed up with the installer, bought the full version.

Where is the DB being installed to? Is it as a subdirectory of the application?

Chris
Q - P = A
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 49
Reputation: Mr.Wobbles is an unknown quantity at this point 
Solved Threads: 2
Mr.Wobbles Mr.Wobbles is offline Offline
Light Poster

Re: Finding directory of VB project dynamically

 
0
  #6
Dec 5th, 2007
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.
Mr.Wobbles~
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 218
Reputation: hkdani is an unknown quantity at this point 
Solved Threads: 24
hkdani's Avatar
hkdani hkdani is offline Offline
Posting Whiz in Training

Re: Finding directory of VB project dynamically

 
0
  #7
Dec 5th, 2007
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)
  1. Public mpProgPath as String

Retrieve the value on loading:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 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)
  1. strDB_PATH = mpProgPath & "\db"
  2. strImage_Path = mpProgPath & "\Images"
Last edited by hkdani; Dec 5th, 2007 at 7:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 3
Reputation: gireeshb22 is an unknown quantity at this point 
Solved Threads: 0
gireeshb22 gireeshb22 is offline Offline
Newbie Poster

Re: Finding directory of VB project dynamically

 
0
  #8
Dec 6th, 2007
Originally Posted by Mr.Wobbles View Post
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)
  1. Public Sub restoreDB()
  2.  
  3. Dim backupName As String
  4. Dim portfolioDBName As String = portfolioDBPath + "portfolioDB.mdb"
  5.  
  6. Dim fdlg As OpenFileDialog = New OpenFileDialog()
  7.  
  8. fdlg.InitialDirectory = portfolioDBPath + "\backup"
  9.  
  10. If fdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
  11. backupName = fdlg.FileName
  12.  
  13. 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?", _
  14. "Confirm Delete", _
  15. MessageBoxButtons.OKCancel, _
  16. MessageBoxIcon.Question, _
  17. MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.OK Then
  18. 'Restore the database from a backup copy.
  19. FileCopy(backupName, portfolioDBName)
  20.  
  21. MsgBox("Database Restoration Successful")
  22. End If
  23. End If
  24.  
  25. End Sub
Hi Wobbles,
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)
  1. Dim objFSO, objFolder, objShell, objTextFile, objFile
  2. Dim strDirectory, strFile, strText
  3. strDirectory = "d:\gb nair"
  4. strFile = "\Summer.txt"
  5. strText = "Book Another Holiday"
  6.  
  7. ' Create the File System Object
  8. Set objFSO = CreateObject("Scripting.FileSystemObject")
  9.  
  10. ' Check that the strDirectory folder exists
  11. If objFSO.FolderExists(strDirectory) Then
  12. Set objFolder = objFSO.GetFolder(strDirectory)
  13. Else
  14. Set objFolder = objFSO.CreateFolder(strDirectory)
  15. WScript.echo "Just created " & strDirectory
  16. End If
  17.  
  18. If objFSO.FileExists(strDirectory & strFile) Then
  19. Set objFolder = objFSO.GetFolder(strDirectory)
  20. Else
  21. Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
  22. WScript.echo "Just created " & strDirectory & strFile
  23. End If
  24.  
  25. Set objFile = Nothing
  26. Set objFolder = Nothing
  27. ' OpenTextFile Method needs a Const value
  28. ' ForAppending = 8 ForReading = 1, ForWriting = 2
  29. Const ForAppending = 8
  30.  
  31. Set objTextFile = objFSO.OpenTextFile _
  32. (strDirectory & strFile, ForAppending, True)
  33.  
  34. ' Writes strText every time you run this VBScript
  35. objTextFile.WriteLine (strText)
  36. objTextFile.Close
  37.  
  38. ' Bonus or cosmetic section to launch explorer to check file
  39. If Err.Number = vbEmpty Then
  40. Set objShell = CreateObject("WScript.Shell")
  41. objShell.Run ("Explorer" & " " & strDirectory & "\")
  42. Else: WScript.echo "VBScript Error: " & Err.Number
  43. End If
  44.  
  45. WScript.Quit
Last edited by cscgal; Dec 6th, 2007 at 2:32 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 49
Reputation: Mr.Wobbles is an unknown quantity at this point 
Solved Threads: 2
Mr.Wobbles Mr.Wobbles is offline Offline
Light Poster

Re: Finding directory of VB project dynamically

 
0
  #9
Dec 6th, 2007
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.
Mr.Wobbles~
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 42
Reputation: Chris147 is an unknown quantity at this point 
Solved Threads: 2
Chris147 Chris147 is offline Offline
Light Poster

Re: Finding directory of VB project dynamically

 
0
  #10
Dec 6th, 2007
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:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub cmdFiles_Click()
  2. MsgBox GetDBLocation
  3. End Sub
  4.  
  5. Private Function GetDBLocation() As String
  6. Dim strDBLocation As String
  7. Dim fso As New FileSystemObject
  8. Dim fldrs As Folder
  9. Dim fldr As Folder
  10. Dim fle As File
  11. Set fldrs = fso.GetFolder("C:\Program Files")
  12. For Each fldr In fldrs.SubFolders
  13. For Each fle In fldr.Files
  14. If fle.ShortName = "YOUR DB NAME" Then
  15. strDBLocation = fle.Name
  16. Goto ExitHere
  17. End If
  18. Next fle
  19. Next fldr
  20. ExitHere:
  21. Set fldrs = Nothing
  22. Set fso = Nothing
  23. If Len(strDBLocation) > 0 Then
  24. GetDBLocation = strDBLocation
  25. Else
  26. GetDBLocation = "Oops! Can't find the DB"
  27. End If
  28. End Function
Q - P = A
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC