Page 1 of 1
读写INI文件怎么指定INI文件的位置在自己程序
Posted: Sun Oct 12, 2008 2:52 am
by liuqi5521
我用如下代码读写设置,发现代码是工作正常了,但是INI文件不知道创建到哪里去了。我想指定INI文件创建在自己程序目录下,要如何做呢?
Code: Select all
// No description
void SafeBoxOpenVDiskDlg::ReadSettings()
{
/* TODO (#1#): Implement SafeBoxOpenVDiskDlg::ReadSettings() */
//读出设置
wxFileConfig fc("vopen");
bool b;
fc.Read("min",&b);
chkMin->SetValue(b);
}
// No description
void SafeBoxOpenVDiskDlg::SaveSettings()
{
/* TODO (#1#): Implement SafeBoxOpenVDiskDlg::SaveSettings() */
//保存设置
wxFileConfig fc("vopen");
fc.Write("min",chkMin->GetValue());
}
Posted: Sun Oct 12, 2008 5:37 am
by chess360
据Cross-Platform GUI Programming
with wxWidgets第20章∶wxWidgets 没有找程序安装路径的函数,因为有的平台(如Linux)没有绝对可靠的方法。
如果是Mac或Windows,可以在App::OnInit()里用wxGetCwd()找到安装目录。
先定义wxFindAppPath函数:
Code: Select all
// Find the absolute path the application has been run from.
wxString wxFindAppPath(const wxString& argv0, const wxString& cwd,
const wxString& appVariableName = wxEmptyString,
const wxString& appName = wxEmptyString);
在程序开始时:
Code: Select all
bool MyApp::OnInit()
{
wxString currentDir = wxGetCwd();
m_appDir = wxFindAppPath(argv[0], currentDir, wxT(“MYAPPDIR”),
wxT(“MyApp”));
...
return true;
}
wxFindAppPath的实现内容在书本的附送光盘,因为作者Julian Smart只公开书本,没有授权公开光盘内容,所以不方便在wxForum贴出,请楼主自行查找。
Posted: Sun Oct 12, 2008 5:55 am
by Utensil
据书上说:
wxRegConfig将会从应用程序名和供应商名称构造一个注册表项,比如前面的例子中将会导致注册表项 HKEY_CURRENT_USER/Software/Acme/MyApp被创建.而如果是Unix系统上的wxFileConfig类,配置文件默认被保存在文件~/.MyApp中. 而在Mac OSX上,则保存在/Library/Preferences/MyApp/Preferences中.这些缺省位置可以通过给wxConfig传递第三个参数来改变。
估计可以用wxFileConfig::GetLocalFile来获得文件所在。如果想保存到自己的位置,似乎wxFileConfig::Save可以?
在Win下还是用注册表吧,比较简单。
-Utensil
Posted: Sun Oct 12, 2008 2:06 pm
by Satervalley
如果要指定文件的话,用一个流来初始化wxFileConfig对象即可,如读取指定INI:
wxFileInputStream is(wxT("your ini file name"));
wxFileConfig fc(is);
fc.Read(...);
Posted: Wed Dec 17, 2008 6:27 am
by huangliujing
对于此问题我也费了一些周折,现将一些代码片段贴在下面,希望对后来者有所帮助。
wxFileInputStream is(wxT("config.ini"));
wxFileConfig *conf = new wxFileConfig(is);
//wxFileConfig *conf = new wxFileConfig(wxEmptyString, wxEmptyString, wxEmptyString, _T("config_huangliujing.ini"), wxCONFIG_USE_GLOBAL_FILE);
// right now the current path is '/'
conf->Write(_T("Group/RootEntry"), _T("Example"));
// go to some other place: if the group(s) don't exist, they will be created
// create an entry in subgroup
conf->Write(_T("Group/Subgroup/SubgroupEntry"), 3);
// '..' is understood
//conf->Write(_T("../GroupEntry"), 2);
wxFileOutputStream os(wxT("config.ini"));
conf->Save(os);
os.Close();
delete conf;
Posted: Wed Dec 17, 2008 10:04 am
by kingkamg
基本不用这个,自己封装xml写配置,当然了也可以用wxXmlSerializer串行化处理