Reputation: 7
I have project in c# it already has some images and videos folders in its solution. I want to know how is it done. so for example i make a new folder to keep some files and i could access them just by their name and probably with folder name under which they are in project also keeping it consistent for debug/release version. I just want to add a text file in a folder say security and just access the file with relative path not absolute path. might be similar to this one but it doesn't help much. How to add the external file to C# application?
Upvotes: 1
Views: 2060
Reputation: 7142
You can use either Directory or DirectoryInfo classes. In my example I use DirectoryInfo. .NET Framework 4.5 added EnumerateFiles method which has superior performance over GetFiles (as in @ZohirSalak CeNa's answer) because it doesn't load all files from directory but lazily one-by-one.
static void ReadFolderFiles()
{
// The parent folder of "my_folder" will be **exe**'s folder:
var dir = new DirectoryInfo("my_folder\\sub_folder");
// Or: DirectoryInfo("my_folder")
foreach(FileInfo file in dir.EnumerateFiles())
{
System.Console.WriteLine(file.Name);
}
}
Upvotes: 0
Reputation:
First of all your application will have to have those folders in the same directory as the .exe
From there you can read it like this :
// This will return all the files Path from that exist in that directory
string[] list = Directory.GetFiles(@".\[directoryName]\");
// From there you can use this line to read the content of a certain file
// In this example i'm reading the first file
// You can use a loop to loop through the files
// There are lot of ways on how to read from a file, leave that for you to find out
File.ReadAllLines(list[0])
Upvotes: 1