Reputation: 232
I have a list of text files that I want to be able to open in a console application.
The output I want is something like:
- List1.txt
- List2.txt
List3.txt
etc
Once I get this output I want a way of being able to call one of those files and have it open via Console.ReadLine();
What I'm doing at the moment is
string[] FileNames = Directory.GetFiles(@"Itemized\", ".txt");
Console.WriteLine(String.Join(Environment.NewLine,FileNames));
This allows me to get as far as getting list that looks like:
Itemized\List1.txt
Itemized\List2.txt
Itemized\List3.txt
If I know the number of files in the folder I can hard code it but the problem I have is that any number of files could be present.
So what im looking for at the moment is a way to append a scaling numeric value to each file and remove the folder-name from the front of it.
I've tried using a for loop to get it to work but can't seem to get my head around it.
Upvotes: 3
Views: 687
Reputation: 5141
Here is something clean and simple:
static void Main(string[] args)
{
string dirFolderPath = string.Format("{0}/{1}", Directory.GetCurrentDirectory(), "Itemized");
DirectoryInfo dir = new DirectoryInfo(dirFolderPath);
if(!dir.Exists)
{
dir.Create();
}
FileInfo[] files = dir.GetFiles("*.txt");
for(int i = 0; i < files.Length; i++)
{
string line = string.Format("\n{0}-{1}", i, files[i].Name);
Console.WriteLine(line);
}
Console.ReadLine();
}
Upvotes: 2
Reputation: 3616
Try something like this
var fileNames = Directory.GetFiles(@"Itemized\", "*.txt").Select(Path.GetFileName).ToArray();
Console.WriteLine(string.Join(Environment.NewLine, fileNames));
Upvotes: 3