Changing Firefox Proxy Programmatically (editing prefs.js)
Issue
I have another kind of problem with Firefox
Description
This is in reference to http://support.mozilla.com/en-US/forum/1/578868 which has been marked answered and closed. There was a change made somewhere between 3.5 and 3.6 that broke the code I wrote, when the proxy line was not in the prefs.js file it assumed no proxy. Now it has to have the proxy line with a 0 to be disabled (missing the line defaults to use system proxy now). So here is a update of my code that I posted in the other thread. Mods - please feel free to copy the contents of this post into the one linked above and delete this one.
Enable FireFox Proxy:
Try
Dim myProfileDirectory() As DirectoryInfo = New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Mozilla\Firefox\Profiles\").GetDirectories("*.default")
Dim myFFPrefFile As String = myProfileDirectory(0).FullName & "\prefs.js"
If File.Exists(myFFPrefFile) Then
' We have a pref file so let's make sure it has the proxy setting
Dim myReader As New StreamReader(myFFPrefFile)
Dim myPrefContents As String = myReader.ReadToEnd
myReader.Close()
' Look for the proxy line
Dim myProxyLineIndex As Integer = myPrefContents.IndexOf("user_pref(""network.proxy.type""")
If myProxyLineIndex > 0 Then
' We found a proxy line so delete it and add our new one to enable. This assumes the proxy server/port are already
' programmed in although you can use the same logic to change those lines
myPrefContents = myPrefContents.Remove(myProxyLineIndex, 38)
myPrefContents = myPrefContents.Insert(myProxyLineIndex, "user_pref(""network.proxy.type"", 1);")
File.Delete(myFFPrefFile)
File.WriteAllText(myFFPrefFile, myPrefContents)
End If
End If
Catch ex As Exception
' Only exception should be a read/write error because the user opened up FireFox so they can be ignored.
End Try
Disable FireFox Proxy:
Try
Dim myProfileDirectory() As DirectoryInfo = New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Mozilla\Firefox\Profiles\").GetDirectories("*.default")
Dim myFFPrefFile As String = myProfileDirectory(0).FullName & "\prefs.js"
If File.Exists(myFFPrefFile) Then
' We have a pref file so lets make sure it has the proxy setting
Dim myReader As New StreamReader(myFFPrefFile)
Dim myPrefContents As String = myReader.ReadToEnd
myReader.Close()
' Look for the proxy line
Dim myProxyLineIndex As Integer = myPrefContents.IndexOf("user_pref(""network.proxy.type""")
If myProxyLineIndex > 0 Then
' We found a proxy line so delete it and add our new one to disable it.
myPrefContents = myPrefContents.Remove(myProxyLineIndex, 38)
myPrefContents = myPrefContents.Insert(myProxyLineIndex, "user_pref(""network.proxy.type"", 0);")
File.Delete(myFFPrefFile)
File.WriteAllText(myFFPrefFile, myPrefContents)
End If
End If
Catch ex As Exception
' Only exception should be a read/write error because the user opened up FireFox so they can be ignored.
End Try
End If
This happened
Every time Firefox opened
Firefox version
3.6.6
Operating system
Windows XP
User Agent
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 ( .NET CLR 3.5.30729)
Plugins installed
- -LogMeIn, Inc. Remote Access Components
- Office Plugin for Netscape Navigator
- RealPlayer(tm) LiveConnect-Enabled Plug-In
- 6.0.12.46
- The QuickTime Plugin allows you to view a wide variety of multimedia content in Web pages. For more information, visit the QuickTime Web site.
- Default Plug-in
- Shockwave Flash 10.1 r53
- 4.0.50524.0
- Windows Presentation Foundation (WPF) plug-in for Mozilla browsers
- NPRuntime Script Plug-in Library for Java(TM) Deploy
- Next Generation Java Plug-in 1.6.0_20 for Mozilla browsers
- Adobe PDF Plug-In For Firefox and Netscape
- DRM Netscape Network Object
- Npdsplay dll
- DRM Store Netscape Plugin