Click here to Skip to main content
15,886,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a multiple texts files that contains only about 5 lines per file. These files contains certain elements that I need on my methods, but since I have a lot of methods I am now having more than 10 texts files in a folder, though having them is non-problematic at the moment, I think having too many text files would pose a problem in the future. So I was wondering if there is a way for me to read a block in a text file basing on tag. say for example I would have this content in a textfile

[object1]
assdfg
asdfghjhgyffhgj
sdghteshjpjkpogkwpotkopg
asdfsfhthhsfgfgsg

[object2]
sadfgjrtsg
gryrhdjdhdhd
retteryhdhdf
sd
asdar


what I'm trying to achieve is to have a method to call in my C# project to read "object1" and it will return data string of all lines below the tag [object1],same goes for object2. It's even ok if the returned string will become 1 line only, but it would be wonderful to retain the line numbers :)

Should there be already existing samples or answers please refer a link. Thanks in advance. :)
Posted

1 solution

So you want, if i understand you, a method to read/write .ini files or something similar. Try this An INI file handling class using C#[^]


EDIT:

Ok,try this to read a whole section:

First,add this dll import:
C#
[DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);


and then,add this method:
C#
public string[] IniReadSection(string Section)
        {
            
            byte[] buffer = new byte[2048];
            GetPrivateProfileSection(Section, buffer, 2048, this.path);
            String[] tmp = Encoding.ASCII.GetString(buffer).Trim('\0').Split('\0');
            
            return tmp;

        }


That will return a string array with all the keys in that section
 
Share this answer
 
v2
Comments
phyxian 30-Jul-14 10:06am    
Sorry for the late response and thanks for that solution! the link was a very helpful one though I noticed that when the readini on that class was called it only returns the line that is directly after the "=" of the key, what I need is to read the whole section(even if the section contains around 10 lines) not just a single line after the key. I tried putting away the key but I get some errors on it. What other options do I have here? BTW I'll be trying to tweak the code.
Pikoh 30-Jul-14 10:18am    
It's been a long time since i last used ini files...but if i remember well if you pass null in the key value,it should return all the section.
phyxian 30-Jul-14 10:24am    
the null value only returns the keyname I used, not the value of the whole section. Tried also with empty string and "" with no results.
Pikoh 30-Jul-14 10:36am    
O.k...see my improved solution and try that code,i think that will help you
phyxian 30-Jul-14 10:59am    
Sorry but I got an error : Cannot implicitly convert type 'string[]' to 'string', though I dont see red lines on the part where the VS told me where the error is, but it says the error is on the "public string[] IniReadSection(string Section)" during build I cant get it to work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900