Accessing path w/o knowing full path name
I need access a file path, but a part of the path is different on each PC.
Mozilla creates a random profile name for each PC it is installed on.
I can get most of the path through environment variables. I need to access the files inside of the default profile, but can't since I'm not sure how to access it.
Anyone have any ideas?
Here's an example of the file path:
'C:\Users\%USERPROFILE%\AppData\Roaming\Mozilla\Firefox\Profiles\xxxxxxxx.default'
The only thing that stays the same with the profile name, is the '.default' part.
Thanks
Re: Accessing path w/o knowing full path name
vb.net Code:
Dim rootPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), _
"Mozilla\Firefox\Profiles")
If IO.Directory.Exists(rootPath) Then
Dim defaultProfilePaths = IO.Directory.GetDirectories(rootPath, "*.default")
'If there can be more than one.
For Each subfolderPath In defaultProfilePaths
MessageBox.Show(subfolderPath)
Next
'If there can be zero or one.
MessageBox.Show(defaultProfilePaths.SingleOrDefault())
'If there is always one.
MessageBox.Show(defaultProfilePaths.Single())
End If
Re: Accessing path w/o knowing full path name
I hope this helps.
EDIT: oopps, too late.
Re: Accessing path w/o knowing full path name
Code:
Dim rootPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), _
"Mozilla\Firefox\")
Dim objIniFile As New IniFile(rootPath & "profiles.ini")
If IO.File.Exists(rootPath & "profiles.ini") = True Then
Dim profile As String = objIniFile.GetString("Profile0", "Path", "")
Dim Formatname As String = profile.Substring(Name.Length + 4)
MsgBox(rootPath & "Profiles\" & Formatname)
End If
Code:
Public Class IniFile
Private Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, ByVal lpFileName As String) _
As Integer
Dim strFilename As String
Public Sub New(ByVal Filename As String)
strFilename = Filename
End Sub
Public Function GetString(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As String) As String
' Returns a string from your INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(512)
intCharCount = GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, strFilename)
If intCharCount > 0 Then GetString = _
Left(objResult.ToString, intCharCount)
End Function
End Class
I didn't code the class I found it online, I also used jmcilhinney rootPath. I'm to late as well.
Re: Accessing path w/o knowing full path name
Thanks for the info!
JMC, your example works perfectly.
Re: Accessing path w/o knowing full path name
Hello. I try to do this on the most recent versions of Mozilla Firefox, on Windows 10. However, it does not work, can you please help me ? I've tried your above code, thanks.
Re: Accessing path w/o knowing full path name
Quote:
Originally Posted by
Andrew2k
it does not work
Then you did it wrong. If you don't show us what you did and tell us what actually happened then we'd need to be psychic to know what the problem is, or a very lucky guesser. The alternative is that you actually provide the information relevant to your problem.
Re: Accessing path w/o knowing full path name
I use this in a module which should return Mozilla Firefox cookies, history, etc.
Code:
Private Function MozillaFireFoxPaths(ByVal pathType As String) As String
Dim rootPath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Mozilla\Firefox\Profiles")
Dim rootPath2 = profileAppDataLocal() & "\Mozilla\Firefox\Profiles"
If Directory.Exists(rootPath) Then
Dim defaultProfilePaths = Directory.GetDirectories(rootPath, "*.default")
For Each subfolderPath In defaultProfilePaths
Select Case pathType
Case "cookies"
Return subfolderPath & "\cookies.sqlite"
Case "webappsstore"
Return subfolderPath & "\webappsstore.sqlite"
Case "downloads"
Return subfolderPath & "\downloads.sqlite"
Case "formhistory"
Return subfolderPath & "\formhistory.sqlite"
Case "cache"
Dim defaultProfilePaths2 = Directory.GetDirectories(rootPath2, "*.default")
For Each subfolderPath2 In defaultProfilePaths2
Return subfolderPath2 & "\cache"
Next
Case "sessionstore.js"
Return subfolderPath & "\sessionstore.js"
Case "sessionstore.bak"
Return subfolderPath & "\sessionstore.bak"
Case Else
Throw New ArgumentException("Exception Occured. Invalid argument for mozillaFireFoxPaths method.")
End Select
Next
End If
End Function
The above code works perfectly on Windows XP, however on Windows Vista, 7 and 10 it does not work, meaning that it does not return the required cookies, history to be deleted.
Re: Accessing path w/o knowing full path name
On W7, the paths are not the same...
under AppData\Roaming\Mozilla\Firefox\Profiles\bj9kvlkk.default-1594028523097 (note that there is a number after default), I have the sqlite files (for example cookies.sqlite) directly in this folder and not in any subfolders and on AppData\Local\Mozilla\Firefox\Profiles\bj9kvlkk.default-1594028523097, I don't have any of these files and subfolders...
Re: Accessing path w/o knowing full path name
I noticed now. How do I modify the code to fit the compatibility with Win7/Win10 ?
I changed for it to get the files instead of getting nonexistent folders in Win7/Win10, this way :
Code:
If Directory.Exists(rootPath) Then
Dim defaultProfilePaths = Directory.GetFiles(rootPath, "*.default")
and still unable to get Cookies, History, etc.
I even changed it to match the exact folder of the Firefox Profile:
Code:
If Directory.Exists(rootPath) Then
Dim defaultProfilePaths = Directory.GetFiles(rootPath, "*.default-release")
Still no remediation.
Re: Accessing path w/o knowing full path name
you can check for the windows version (see here ) and create the functions with different path and co. depending on the version
Re: Accessing path w/o knowing full path name
I only need compatibility with Windows 7, 8 and 10. There must be a single way to do it for all of those. How can I achieve that in VB.NET for Windows 7/8/10 ? Thanks in advance.
Re: Accessing path w/o knowing full path name
Can someone please provide a reliable solution to this issue?
Thank you.
Re: Accessing path w/o knowing full path name
Does Firefox store the root path in the Registry? If so, you should get the root path from the Registry first and then extend that using Path.Combine for each specific path.
Re: Accessing path w/o knowing full path name
Unfortunately, Firefox does not seem to store the root path into the registry as far as I know.
However, it stores cookies.sqlite and other history data in %APPDATA%\Roaming\Mozilla \Firefox\Profiles\ xxxx.default <- - - this 'xxx' is different on each PC. Inside this, there are the files which need to be deleted:cookies.sqlite, etc. I can't get it to work with the above code in Windows 7 and 10.