标准 Windows .ini 文件是否允许注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1378219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:41:33  来源:igfitidea点击:

Do standard windows .ini files allow comments?

windowscommentsini

提问by Roddy

Are comments allowed in Windows ini files? (...assuming you're using the GetPrivateProfileStringapi functions to read them...)

Windows ini 文件中是否允许注释?(...假设您正在使用GetPrivateProfileStringapi 函数来读取它们...)

[Section]
Name=Value   ; comment

; full line comment

And, is there a proper spec of the .INI file format anywhere?

并且,是否有适当的 .INI 文件格式规范?

Thanks for the replies- However maybe I wasn't clear enough. It's only the format as read by Windows API Callsthat I'm interested in. I know other implementations allow comments, but it's specifically the MS Windows spec and implementation that I need to know about.

感谢您的回复- 不过也许我还不够清楚。这只是我感兴趣的 Windows API 调用读取的格式。我知道其他实现允许评论,但特别是我需要了解的 MS Windows 规范和实现。

回答by Ian Boyd

Windows INI API support for:

Windows INI API 支持:

  • Line comments: yes, using semi-colon ;
  • Trailing comments:No
  • 行注释:是的,使用分号;
  • 尾随评论:

The authoritative source is the Windows API function that reads values out of INI files

权威来源是从 INI 文件中读取值的 Windows API 函数

GetPrivateProfileString

Retrieves a string from the specified section in an initialization file.

获取私有配置文件字符串

从初始化文件中的指定部分检索字符串。

The reason "full line comments"work is because the requested value does not exist. For example, when parsing the following inifile contents:

之所以“全行注释”工作是因为请求的值不存在。例如,在解析以下ini文件内容时:

[Application]
UseLiveData=1
;coke=zero
pepsi=diet   ;gag
#stackoverflow=splotchy

Reading the values:

读取值:

  • UseLiveData: 1
  • coke: not present
  • ;coke: not present
  • pepsi: diet ;gag
  • stackoverflow: not present
  • #stackoverflow: splotchy
  • UseLiveData1
  • coke:不存在
  • ;coke:不存在
  • pepsidiet ;gag
  • stackoverflow:不存在
  • #stackoverflowsplotchy

Update: I used to think that the number sign (#) was a pseudo line-comment character. The reason using leading # works to hide stackoverflowis because the name stackoverflowno longer exists. And it turns out that semi-colon (;) isa line-comment.

更新:我曾经认为数字符号 (#) 是一个伪行注释字符。使用前导 # 隐藏的原因stackoverflow是因为名称stackoverflow不再存在。事实证明,分号 ( ;)行注释。

But there is no support for trailing comments.

但是不支持尾随评论。

回答by RoguePlanetoid

I have seen comments in INI files, so yes. Please refer to this Wikipedia article. I could not find an official specification, but that is the correct syntax for comments, as many game INI files had this as I remember.

我在 INI 文件中看到过评论,所以是的。请参考这篇维基百科文章。我找不到官方规范,但这是评论的正确语法,因为我记得很多游戏 INI 文件都有这个。

Edit

编辑

The API returns the Value and the Comment (forgot to mention this in my reply), just construct and example INI file and call the API on this (with comments) and you can see how this is returned.

API 返回值和注释(在我的回复中忘记提及这一点),只需构建和示例 INI 文件并在此(带有注释)上调用 API,您就可以看到它是如何返回的。

回答by Antoine Meltzheim

USE A SEMI-COLON AT BEGINING OF LINE --->> ; <<---

在行的开头使用分号 --->> ; <<---

Ex.

前任。

; last modified 1 April 2001 by John Doe
[owner]
name=John Doe
organization=Acme Widgets Inc.

回答by Koekiebox

Yes. Have a look at Wikipediaand Cloanto Implementation of INI File Format(see bottom of page).

是的。看看维基百科INI 文件格式的 Cloanto 实现(见页面底部)。