读写INI文件怎么指定INI文件的位置在自己程序

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
liuqi5521
Earned some good credits
Earned some good credits
Posts: 103
Joined: Thu Apr 03, 2008 5:35 am
Location: China
Contact:

读写INI文件怎么指定INI文件的位置在自己程序

Post by liuqi5521 » Sun Oct 12, 2008 2:52 am

我用如下代码读写设置,发现代码是工作正常了,但是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());
}

chess360
Earned a small fee
Earned a small fee
Posts: 15
Joined: Thu Oct 02, 2008 12:07 pm

Post by chess360 » Sun Oct 12, 2008 5:37 am

据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贴出,请楼主自行查找。

Utensil
Moderator
Moderator
Posts: 423
Joined: Sun Feb 03, 2008 11:38 am
Location: China

Post by Utensil » Sun Oct 12, 2008 5:55 am

据书上说:

wxRegConfig将会从应用程序名和供应商名称构造一个注册表项,比如前面的例子中将会导致注册表项 HKEY_CURRENT_USER/Software/Acme/MyApp被创建.而如果是Unix系统上的wxFileConfig类,配置文件默认被保存在文件~/.MyApp中. 而在Mac OSX上,则保存在/Library/Preferences/MyApp/Preferences中.这些缺省位置可以通过给wxConfig传递第三个参数来改变。

估计可以用wxFileConfig::GetLocalFile来获得文件所在。如果想保存到自己的位置,似乎wxFileConfig::Save可以?

在Win下还是用注册表吧,比较简单。

-Utensil
In fascination of creating worlds by words, and in pursuit of words behind the world.

On Github: http://utensil.github.com
Technical Blog in Chinese: http://utensil.iteye.com/

Satervalley
Knows some wx things
Knows some wx things
Posts: 47
Joined: Fri Dec 14, 2007 1:10 am

Post by Satervalley » Sun Oct 12, 2008 2:06 pm

如果要指定文件的话,用一个流来初始化wxFileConfig对象即可,如读取指定INI:
wxFileInputStream is(wxT("your ini file name"));
wxFileConfig fc(is);

fc.Read(...);

huangliujing
In need of some credit
In need of some credit
Posts: 1
Joined: Mon Dec 08, 2008 5:59 am
Location: Beijing China

Post by huangliujing » Wed Dec 17, 2008 6:27 am

对于此问题我也费了一些周折,现将一些代码片段贴在下面,希望对后来者有所帮助。
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;

kingkamg
I live to help wx-kind
I live to help wx-kind
Posts: 187
Joined: Tue Apr 08, 2008 1:45 pm

Post by kingkamg » Wed Dec 17, 2008 10:04 am

:D 基本不用这个,自己封装xml写配置,当然了也可以用wxXmlSerializer串行化处理

Post Reply