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

Comments

Please log in to comment

Answers

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.
Answered 08/24/2015 by: jagadeish
Red Belt

  • 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??
Please log in to comment
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.
Answered 08/24/2015 by: VBScab
Red Belt

  • 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.
Please log in to comment
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.
Answered 08/24/2015 by: VBScab
Red Belt

Please log in to comment
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.
Answered 08/24/2015 by: VBScab
Red Belt

  • 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.
Please log in to comment
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...
Answered 08/24/2015 by: VBScab
Red Belt

  • [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.
Please log in to comment
0
...because then you would've found 'INF File Processing Functions'.

Anything else I can spoon-feed you?
Answered 08/24/2015 by: VBScab
Red Belt

  • It was closer to a spork-feed since you did not provide a link to MSDN. Completely your fault LOL
Please log in to comment
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.
Answered 08/24/2015 by: EdT
Red Belt

  • 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 :).
Please log in to comment
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?
Answered 08/24/2015 by: VBScab
Red Belt

  • I'm waiting for the procmon comments to come :-)
Please log in to comment
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.
Answered 08/25/2015 by: VBScab
Red Belt

Please log in to comment
Answer this question or Comment on this question for clarity