How to list the contents in files in a folder under a particular heading according to alphabetical order using c# without using database..
anybody know please help me
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Wim SturkenboomRank #2,527 · 554 reputationPosted Sep 17, 2014, 2:53 AM EDT
The program reads a file and uses a switch to determine between header and content. If a header is detected, it will set the header 'type'. If content is detected, it will add the content line to one of the lists depending on the current header type.
class Program
{
enum HEADINGTYPE
{
BASICS,
PRESENTATION,
CAUSES,
EXAMINATION,
NOTYETDEFINED
}
static void Main(string[] args)
{
string[] strFiles = System.IO.Directory.GetFiles(".\\diseases");
List
lstFiles.Sort();
// create lists to hold info
List
List
List
List
// loop through the files
for (int filecnt = 0; filecnt < lstFiles.Count; filecnt++)
{
// set heading type to not defined
HEADINGTYPE hdt = HEADINGTYPE.NOTYETDEFINED;
// read file
using (System.IO.TextReader rdr = System.IO.File.OpenText(lstFiles[filecnt]))
{
string line;
while ((line = rdr.ReadLine()) != null)
{
switch (line)
{
case "BASICS":
hdt = HEADINGTYPE.BASICS;
break;
case "CLINICAL PRESENTATION":
hdt = HEADINGTYPE.PRESENTATION;
break;
case "CLINICAL EXAMINATION":
hdt = HEADINGTYPE.EXAMINATION;
break;
case "CAUSES":
hdt = HEADINGTYPE.CAUSES;
break;
default:
switch (hdt)
{
case HEADINGTYPE.BASICS:
lstBasics.Add(line);
break;
case HEADINGTYPE.CAUSES:
lstCauses.Add(line);
break;
case HEADINGTYPE.EXAMINATION:
lstExamination.Add(line);
break;
case HEADINGTYPE.PRESENTATION:
lstPresentation.Add(line);
break;
default:
// the heading type is not set yet
break;
} // end_of_switch(hdt)
break;
} // end_of_switch(line)
}
} // end_of_using (System.IO.TextReader rdr= System.IO.File.OpenText(lstFiles[filecnt]))
}
lstBasics.Sort();
lstPresentation.Sort();
lstExamination.Sort();
lstCauses.Sort();
Console.WriteLine("== basics");
for (int cnt = 0; cnt < lstBasics.Count; cnt++)
{
Console.WriteLine(lstBasics[cnt]);
}
Console.WriteLine("== presentation");
for (int cnt = 0; cnt < lstPresentation.Count; cnt++)
{
Console.WriteLine(lstPresentation[cnt]);
}
Console.WriteLine("== examination");
for (int cnt = 0; cnt < lstExamination.Count; cnt++)
{
Console.WriteLine(lstExamination[cnt]);
}
Console.WriteLine("== causes");
for (int cnt = 0; cnt < lstCauses.Count; cnt++)
{
Console.WriteLine(lstCauses[cnt]);
}
Console.WriteLine("== done");
Console.ReadLine();
}
}
Wim SturkenboomRank #2,527 · 554 reputationPosted Sep 15, 2014, 9:42 PM EDT
static void Main(string[] args)
{
string[] strFiles = System.IO.Directory.GetFiles("c:\\");
List
lstFiles.Sort();
for (int filecnt = 0; filecnt < lstFiles.Count; filecnt++)
{
Console.WriteLine(System.IO.File.ReadAllText(lstFiles[filecnt]));
}
}
It gets the files in a directory and stores them into an array; you can write your own sorting algorithm for alphabetical ordering. I convert the string array to a list so it can be sorted.
The for-loop is where the processing of the files is done; for you to implement.
reshma chandran9 reputationPosted Sep 15, 2014, 11:21 PM EDT
Wim SturkenboomRank #2,527 · 554 reputationPosted Sep 16, 2014, 12:44 AM EDT
headingA headingB headingC
file1 abc def xyz
file2 123 456 890
file3 1a1 2b2 3c3
I can't advise on how to pull the specific data for a column (heading) without more information about it. If that's your problem, you have to provide some sample data.
reshma chandran9 reputationPosted Sep 16, 2014, 6:44 PM EDT
Wim SturkenboomRank #2,527 · 554 reputationPosted Sep 16, 2014, 8:16 PM EDT
reshma chandran9 reputationPosted Sep 16, 2014, 8:35 PM EDT
BASICS
•Abnormal deficiency of sweat
•Other characteristics malaise, easyfatiguability, headache, nausea, warmth, dry skin, tachcardia
CLINICAL PRESENTATION
•Skin deficient sweat
•Malaise
•Easy fatiguability
•Headache
•Nausea
•Warmth
•Dry skin
CLINICAL EXAMINATION
•Skin deficient sweat
•Tachycardia
CAUSES
•Senile skin
•Radiodermatitis
•Acrodermatitis chronica atrophica
•Scleroderma
•Lichen sclerosis et atrophica
•Spinal cord lesions
•Fabry's disease
•Quinacrine toxicity
•Miliaria
•Sympathectomy
reshma chandran9 reputationPosted Sep 18, 2014, 4:55 AM EDT
sir,
How can I use this on a windows application.I have tried it on console application.window is coming .If we give an input it wil coming back to the program.