Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building Dynamic Code 1

Status
Not open for further replies.

Haunter

Programmer
Jan 2, 2001
379
US
I am from a PERL back ground and we are able to build code dynamically. I was wondering if this is possible in C#. What I want to do is change the display of a forms options based on which files are present in a directory.

Specifically I want to open a text file and make its contents displayed as a check box option. If selected the contents of the text file will to be included in a word.doc of my choice.

I will not know in advance how many text files will exist. I am trying to make the application more user configurable. In PERL we use simple text files a lot and this is the reason for that choice, a better alternative would be good.

Bear in mind I am a novice C# programmer. I understand classes but am foggy on .dll and more advanced stuff but a point in the right direction would help.


haunter@battlestrata.com
 
So you want something like:

[] File 1
[] File 2
[] File 3
...

or:

[] Text from file 1 that may go
on for many lines...
[] Text from file 2 that may go
on for many lines...
[] Text from file 3 that may go
on for many lines...
...

If so, this can be done fairly easily, and you don't need to build any code dynamically (unless your files are code and you are using this program to 'build code dynamically').

For the first option, you can run through the list of files creating a new checkbox control each time and add it to any area you would like. Then when you switch folders, just get rid of them and start over (you will have kept them in an array of course). If I am totally of base here, let me know what I thought wrong.

"Programming is like sex, one mistake and you have to support it forever."

John

johnmc@mvmills.com
 
Yes this is what I am looking for. I just dont know what is the proper way to handle it in C#. I dont know if a class is the answer or if this is something that is already done in a dll file. I also dont know the proper syntax for just opening and reading a text file. I am looking at that now.

Most importantly I am trying to learn the proper way to handle things in C#. I am new to the language ad trying to learn to code it properly. PERL is more procedural than OO. I am just trying to bring in easy configuration for the user.


haunter@battlestrata.com
 
Hi Haunter,
here's a small example how to get all files from a directory:

DirectoryInfo currDir = new DirectoryInfo(path);
FileInfo[] files = currDir.GetFiles();
//gets all the files in the directory
OR
string searchPattern = "*.txt";
FileInfo[] files = currDir.GetFile(searchPattern);
//gets all .txt files

foreach(FileInfo file in files)
//loops through the array of files
{
//...
}

You can also use the FileInfo class to open files for reading:

FileStream fs = file.OpenRead();

and then use fs.Read for reading a number of bytes.

I hope this helps. You can also take a look on the microsoft developer network for some more info about the used objects above (
 
Thanks for the syntax. What is obvious to one may be very ellusive to another. ty

Star for the extra effort!


haunter@battlestrata.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top