Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I want to do:
I want to read a .CFG file and split the variables and values into text boxes. The .CFG file has several sections or main groups.
I would like to automatically create textboxes in a loop depending on how many lines there are and then insert the values in there

What does this look like?
[GRAPHICS]
SCREEN_CAPTURE_MAX_JOBS=10
VIDEO_CAPTURE_WIDTH=720
VIDEO_CAPTURE_HEIGHT=480
VIDEO_CAPTURE_FPS=30
ShowDeviceLostWarning=0

[WEATHER]
CLOUD_FACING_MODE=0
MinGustTime=10
MaxGustTime=500
MinGustRampSpeed=1
MaxGustRampSpeed=200
MinVarTime=5
MaxVarTime=50

[XXXX]
XXXX=XXXX
XXXX=XXXX
XXXX=XXXX
.......

What I have tried:

I don't know how to read and separate the variables and values individually and then insert them into the text boxes. These should also be section separated ([XXX]).


I already read the CFG file.

I had thought about a loop that counts the individual lines and then creates so many text boxes.
Posted
Updated 2-May-21 4:42am
v2
Comments
Richard MacCutchan 2-May-21 10:48am    
Something like a DatagridView would be much more useful than lots of TextBoxes.
Richard MacCutchan 2-May-21 11:39am    
Read the file one line at a time. If the line is of the form "[sometext]" then it is a section header. Create a Dictionary<string, Dictionary> which will hold one Dictionary<string, string> for each section name. Then for each of the lines below the sction, use String.Split to separate the two parts, and and them as a key,value pair to the relevant dictionary. Once you have captured all the data it is a fairly simple matter to display it in some appropriate cotrol type on your form.
If not then assume it is a key,value pair. So you could create Dictinaries of key,value pairs, using the section names as keys to the List of Dis=ctionaries.
zTrusted WF 2-May-21 15:07pm    
Thank you for your answer and help!

1 solution

That is your standard, everyday, outdated .INI file. There's plenty of articles here on CP that show you how to deal with them. All it takes is a little search[^].
 
Share this answer
 
Comments
zTrusted WF 2-May-21 10:44am    
I don't understand. It's not an .INI file.
Dave Kreskowiak 2-May-21 10:45am    
Yes it is. The file extension means nothing. The content is still an INI file.
zTrusted WF 2-May-21 10:52am    
I would not have asked the question if I had found an answer. Unfortunately, your answer doesn't help me.
Dave Kreskowiak 2-May-21 10:54am    
Frankly, textbox-per-line is ridiculous, but whatever.

If you can read the file and represent the data as sections with collections of key/value pairs, you've already got everything you need to "generate" an appropriate number of textboxes.

So, I guess the question comes down to how are you representing the content of the file in your app once you read the file?

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