/build/static/layout/Breadcrumb_cap_w.png

How to check section exists in INF file

I wrote the below code to read / write contents to inf file for a section.
But how to check whether that particular section exists or not.As this code is returning empty string both for section/value in the section doesn't exist.How to differentiate and check both.
 public class IniFile
{
public string path;

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key,string def, StringBuilder retVal,
int size,string filePath);

///
/// INIFile Constructor.
///

///
public IniFile(string INIPath)
{
path = INIPath;
}
///
/// Write Data to the INI File
///

///
/// Section name
///
/// Key Name
///
/// Value Name
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}

///
/// Read Data Value From the Ini File
///

///
///
///
///
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,
255, this.path);
return temp.ToString();

}
}


0 Comments   [ + ] Show comments

Answers (9)

Posted by: jagadeish 8 years ago
Red Belt
0
[Section]
Key=Value

When you search for a particular "Section" in the IniFile, If the "Section" itself does not exist then how "Key" would be exist for that section.

Comments:
  • My question is different.WHen you search for a string in a section.There will be 2 cases.

    1. Section doesn't exist.
    2. String key not exists and section exists.

    In both cases the method will return empty string.

    How will you differentiate then which is missing?? - ur00361883 8 years ago
Posted by: anonymous_9363 8 years ago
Red Belt
0
According to MSDN, if the section doesn't exist, the function returns all the section names in the buffer.

There's an INI class file for VBS knocking about which would provide you with some clues on handling INIs.

Comments:
  • I tried to use this API instead of native one, but it is not reading the inf file(but able to read ini).It is unable to parse inf files of system drivers. - ur00361883 8 years ago
Posted by: anonymous_9363 8 years ago
Red Belt
0
Apologies: I mis-read. There is no native means to test for a no -existent section. You're going to have to resort to good old-fashioned string parsing.
Posted by: anonymous_9363 8 years ago
Red Belt
0
If the INF is just text, it wouldn't matter what its intended target was.

That brings me to a thought: make sure the INF is saved in plain, not Unicode or equally strangulated form of text. If it is, you need to use GetPrivateProfileStringW.

Comments:
  • That is the problem, we can't change the format of driver files :). And because of this other API are not working except the native one.
    GetPrivateProfileStringW - Same function I am using.

    WHen you search for a string in a section.There will be 2 cases.

    1. Section doesn't exist.
    2. String key not exists and section exists.

    In both cases the method will return empty string. - ur00361883 8 years ago
Posted by: anonymous_9363 8 years ago
Red Belt
0
Same function I am using.
Nope. The function you're using is a legacy one. You should be using GetPrivateProfileStringW for Unicode files and GetPrivateProfileStringA for ANSI files.

I thought you might've actually *looked* at MSDN after I'd mentioned it...

Comments:
  • [DllImport("kernel32", SetLastError=true)] private static extern int GetPrivateProfileStringW(string pSection, string pKey, string pDefault, byte[] prReturn, int pBufferLen, string pFile);

    This DLL import is not working.Could you please guide me on this
    Or provide the msdn link. - ur00361883 8 years ago
Posted by: anonymous_9363 8 years ago
Red Belt
0
...because then you would've found 'INF File Processing Functions'.

Anything else I can spoon-feed you?

Comments:
  • It was closer to a spork-feed since you did not provide a link to MSDN. Completely your fault LOL - h2opolo25 8 years ago
Posted by: EdT 8 years ago
Red Belt
0
There appears to be some interchangeability in the use of "INIfile" and "INFfile".
However, it is not rocket science to parse a text file line by line and then test for the existence of a particular section heading. The differentiator is that section headings are in square brackets, so if you check the line for the existence of the character "[" you will know whether it is a section heading or not. If it is, set it all to uppercase (just in case) and then look for the required string within the line.  If it is found, you know the section exists, so the next step is to check each subsequent line (set to uppercase) for a "[" matching entry, and then for a required text section entry until you encounter either a match on the text entry or encounter the next "[" which means you have finished checking that section.  Sounds more complicated than it is in VBScript.

Comments:
  • I tried it first and it working for me with normal ini files but it is failing for driver related inf files as it unable to parse correctly.So I shifting to this native library and struck in the middle :). - ur00361883 8 years ago
Posted by: anonymous_9363 8 years ago
Red Belt
0
>Sounds more complicated than it is in VBScript.
...which our friend would find out if he sought out the INI class I mentioned :-) I remember there's a function in it called 'CheckBasics' so i wonder if a search for 'INI class CheckBasics' would turn up anything?

Comments:
  • I'm waiting for the procmon comments to come :-) - Pressanykey 8 years ago
Posted by: anonymous_9363 8 years ago
Red Belt
0
Ha, ha, ha! No, no ProcMon comments this time, mainly because it wouldn't help in this situation.

@OP, you're back to good old string-parsing, I'm afraid.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ