.net 更改Firefox代理设置?

.net 更改Firefox代理设置?,.net,firefox,vbscript,proxy,.net,Firefox,Vbscript,Proxy,如何从vb脚本或.net脚本更改Firefox的代理设置?如果有人能告诉我怎么做,那就太好了。你可以通过写入prefs.js文件来完成,但是,我的理解是,这个文件只在启动时加载,Firefox可能会在关闭时覆盖你的更改,所以我认为唯一安全的方法是: 关闭所有Firefox进程 更改prefs.js 启动Firefox 本代码项目文章应向您展示如何从vbscript关闭进程: 您可以使用来读取/写入文件 我认为这样的东西可以让它重新启动: Set shell = CreateObject("WSc

如何从vb脚本或.net脚本更改Firefox的代理设置?如果有人能告诉我怎么做,那就太好了。

你可以通过写入
prefs.js
文件来完成,但是,我的理解是,这个文件只在启动时加载,Firefox可能会在关闭时覆盖你的更改,所以我认为唯一安全的方法是:

  • 关闭所有Firefox进程
  • 更改
    prefs.js
  • 启动Firefox
  • 本代码项目文章应向您展示如何从vbscript关闭进程:

    您可以使用来读取/写入文件

    我认为这样的东西可以让它重新启动:

    Set shell = CreateObject("WScript.Shell")
    shell.Run Chr(34) & "PathToFireFox.exe" & Chr(34), 1, false
    
    /*此程序将找到用户应用程序数据的路径,并获取路径
    (\Mozilla\Firefox\Profiles\*.default)并从文件prefs.js中查找代理*/
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #定义最大值8192
    使用名称空间std;
    wstring ReadProxyFromFile(wstring文件名);
    wstring ParseAndGetProxyDetails(wstring strLine,wstring字段);
    wstring ReadINIFileAndGetFirefoxProfilePath(wstring&profileFilePath);
    /// 
    ///此函数用于将wstring转换为字符串。
    /// 
    字符串到字符串(wchar\u t const*pwch,UINT代码页)
    {
    //分配适当的空间
    int sizeRequired=WIDECHARTOMULITYTE(代码页,0,pwch,-1,NULL,0,NULL,NULL);
    char*ach=新字符[sizeRequired+1];
    //转换它
    宽图表多字节(代码页面,0,pwch,-1,ach,所需大小,NULL,NULL);
    //将其放入返回值中
    字符串ret(ach);
    //清理
    删除[]ach;
    //返回新字符串
    返回ret;
    }
    /// 
    ///此函数使用分隔符将字符串拆分为字段。
    /// 
    向量拆分(字符串常量和字符串常量,字符常量*sep)
    {
    string str=str_u.c_str();
    大小(sep)len=::strlen(sep);
    向量ret;
    对于(大小i=str.find(sep);i!=str.npos;i=str.find(sep))
    {
    后推(str.substr(0,i));
    str=str.substr(i+sep_len,str.npos);
    }
    如果(str.length()>0)
    后推回(str);
    返回ret;
    }
    /// 
    ///此函数用于将字符串转换为wstring。
    /// 
    wstring ToWStr(字符串常量和x,UINT代码页)
    {
    //分配适当的空间
    int sizeRequired=MultiByteToWideChar(code_page,0,x.c_str(),-1,NULL,0);
    wchar_t*ach=新的wchar_t[sizeRequired+1];
    //转换它
    MultiByteToWideChar(代码页,0,x.c_str(),-1,ach,需要大小);
    //将其放入返回值中
    WST环网(ach);
    //清理
    删除[]ach;
    //返回新字符串
    返回ret;
    }
    int main()
    {
    LPWSTR wszPath=NULL;
    HRESULT-hr;
    wstring strPath;
    hr=SHGetKnownFolderPath(FOLDERID\u RoamingAppData、KF\u FLAG\u CREATE、NULL和wszPath);
    如果(成功(hr))
    {
    strPath=wszPath;
    strPath+=L“\\Mozilla\\Firefox”;
    wstring profilepath=strPath+L“\\profiles.ini”;
    wstring strCurrentProfile=ReadINIFileAndGetFirefoxProfilePath(profileFilePath);
    wstring prefsPath=strPath+L“\\”+strCurrentProfile+L“\\prefs.js”;
    wstring strProxy=ReadProxyFromFile(prefsPath);
    如果(strProxy.length()>0)
    {
    wcout
    
     /*This program will find the path to application data of user,and get the path 
    (\Mozilla\Firefox\Profiles\*.default) and find proxy from the file prefs.js  */
    
    
    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <windows.h>
    #include <string>
    #include <shlobj.h>
    #include <winerror.h>
    #include <vector>
    
    #define MAX 8192
    using namespace std;
    
    wstring ReadProxyFromFile(wstring fileName);
    wstring ParseAndGetProxyDetails (wstring strLine, wstring field);
    wstring ReadINIFileAndGetFirefoxProfilePath( wstring &profileFilePath );
    
    /// 
    /// This function converts wstring to string.
    /// 
    string ToString(wchar_t const * pwch, UINT code_page)
    {
      // Allocate the appropriate space
      int sizeRequired = WideCharToMultiByte( code_page, 0, pwch, -1, NULL, 0,  NULL, NULL);
      char * ach = new char[sizeRequired + 1];
    
      // Convert it
      WideCharToMultiByte( code_page, 0, pwch, -1, ach, sizeRequired, NULL, NULL);
    
      // Put it into the return value
      string ret(ach);
    
      // Clean up
      delete[] ach;
    
      // Return the new string
      return ret;
    }
    
    /// 
    /// This function splits a string into fields using a separator.
    /// 
    
    vector<string> Split(string const & str_, char const * sep)
    {
      string str = str_.c_str();
      size_t sep_len = ::strlen(sep);
    
      vector<string> ret;
      for (size_t i = str.find(sep); i != str.npos; i = str.find(sep))
      {
        ret.push_back(str.substr(0, i));
        str = str.substr(i + sep_len, str.npos);
      }
      if (str.length() > 0)
        ret.push_back(str);
      return ret;
    }
    
    /// 
    /// This function converts string to wstring.
    /// 
    wstring ToWStr(string const& x, UINT code_page )
    {
      // Allocate the appropriate space
      int sizeRequired = MultiByteToWideChar(code_page, 0, x.c_str(), -1, NULL, 0);
      wchar_t * ach = new wchar_t[sizeRequired + 1];
    
      // Convert it
      MultiByteToWideChar(code_page, 0, x.c_str(), -1, ach, sizeRequired);
    
      // Put it into the return value
      wstring ret(ach);
    
      // Clean up
      delete[] ach;
    
      // Return the new string
      return ret;
    }
    
    int main()
    {
      LPWSTR wszPath = NULL;
      HRESULT hr;
      wstring strPath;
      hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData , KF_FLAG_CREATE, NULL, &wszPath);
    
      if (SUCCEEDED(hr))
      {
        strPath = wszPath; 
        strPath += L"\\Mozilla\\Firefox";
    
        wstring profileFilePath = strPath + L"\\profiles.ini";
        wstring strCurrentProfile =  ReadINIFileAndGetFirefoxProfilePath(profileFilePath);
        wstring prefsPath = strPath + L"\\" + strCurrentProfile + L"\\prefs.js";
    
        wstring strProxy = ReadProxyFromFile(prefsPath);
        if (strProxy.length()>0)
        {
          wcout << "Proxy is " <<strProxy <<endl;
        }
        return 0;
      }
    }
    
    wstring ReadINIFileAndGetFirefoxProfilePath( wstring &profileFilePath ) 
    {
      // reading each section from ini file
      LPWSTR pSecNames= new WCHAR[1024];
      int nSectionNum = 1;
      wstring strAllSectionNames;
    
      DWORD retVal = GetPrivateProfileSectionNames(pSecNames,1024,profileFilePath.c_str());
    
      if (retVal)
      {
        for (unsigned int i =0; i < retVal; i++)
        {
          if (pSecNames[i] == NULL)
          {
            strAllSectionNames += L"_";
          }
          else
          {
            strAllSectionNames += pSecNames[i];
          }
        }
      delete pSecNames;
    
      string sectionNames = ToString(strAllSectionNames.c_str(),CP_UTF8);
      vector <string> sectionNamesVector = Split(sectionNames.c_str(),"_");
    
      wstring strSectionName;
      for ( int it = 0 ; it < sectionNamesVector.size(); it++ )
      {
        strSectionName = ToWStr(sectionNamesVector.at(it),CP_UTF8);
        wcout << nSectionNum << ". Section name: " << strSectionName;
        LPWSTR pProfile= new WCHAR[1024];
        DWORD active = GetPrivateProfileString(strSectionName.c_str(),L"Default",NULL,pProfile,MAX,profileFilePath.c_str());
        if(active)
        {
          GetPrivateProfileString(strSectionName.c_str(),L"Path",NULL,(LPWSTR)pProfile,MAX,profileFilePath.c_str());
          wstring strPath = pProfile;
          delete pProfile;
          return strPath;
        }
        delete pProfile;
        nSectionNum++;
      }
     }
    }
    
    
    wstring ReadProxyFromFile(wstring fileName)
    {
      wstring proxyType;
      wstring proxyHttp;
      wstring httpPort;
      wstring noProxiesOn;
      wstring autoconfigUrl;
      wstring proxySettings; 
      wstring field;
      int nProxyType;
    
      wifstream myfile;
      wstring strLine;
      size_t pos;
    
      myfile.open (fileName.c_str());
      if (myfile.is_open())
      {
        while (!myfile.eof())
        {
          getline(myfile,strLine);
          if (!strLine.find(L"user_pref"))
          {
            pos = strLine.find_first_of(L", ");
            field = strLine.substr(11,pos-12);
    
            if (field == L"network.proxy.type")
            {
              proxyType  = ParseAndGetProxyDetails(strLine, field);
              nProxyType = _wtoi(proxyType.c_str());
            }
            if (field == L"network.proxy.http")
            {
              proxyHttp  = ParseAndGetProxyDetails(strLine, field);
            }
            if (field == L"network.proxy.http_port")
            {
              httpPort  = ParseAndGetProxyDetails(strLine, field);
            }
            if (field == L"network.proxy.autoconfig_url")
            {
              autoconfigUrl  = ParseAndGetProxyDetails(strLine, field);
            }
            if (field == L"network.proxy.no_proxies_on")
            {
              noProxiesOn  = ParseAndGetProxyDetails(strLine, field);
            }
          }
        }
      }
      myfile.close();
    
      switch (nProxyType)
      {
      case 0: // no proxy
        proxySettings = L"FF:";
        break;
      case 1: // manual proxy
        proxySettings = L"FF:PXY:"+proxyHttp+L":"+httpPort;
        break;
      case 2: //autoconfig_url
        proxySettings = L"FF:URL:"+autoconfigUrl;
        break;
      case 4: // auto proxy
        proxySettings = L"FF:";
        break;
      default: // system proxy
        proxySettings = L"FF::";
        break;
      }
    
      return proxySettings;
    }
    
    wstring ParseAndGetProxyDetails (wstring strLine, wstring field)
    {
      size_t position;
      wstring proxyDetail;
      position = strLine.find(field);
      if (position != string::npos)
      {
        proxyDetail = strLine.substr(position);
        position = proxyDetail.find_first_of(L",");
        proxyDetail = proxyDetail.substr(position+1);
        position = proxyDetail.find_first_of(L");");
        proxyDetail = proxyDetail.substr(0,position);
      }
      return proxyDetail;
    }