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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
reshma chandranPosted Sep 18, 2014, 8:55 AM
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.
Wim SturkenboomPosted Sep 17, 2014, 6:53 AM
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();
}
}
reshma chandranPosted Sep 17, 2014, 12:35 AM
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
Wim SturkenboomPosted Sep 17, 2014, 12:16 AM
reshma chandranPosted Sep 16, 2014, 10:44 PM
Wim SturkenboomPosted Sep 16, 2014, 4:44 AM
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 chandranPosted Sep 16, 2014, 3:21 AM
Wim SturkenboomPosted Sep 16, 2014, 1:42 AM
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.