I have a C# project (Windows Console Application). I have created a folder named Data inside project. There are two text files inside folder Data.

How can I read the text files from "Data" folder. I tried below things.

string[] files = File.ReadAllLines(@"Data\Names.txt") 

It is thowing error that file not found.

I have checked some Stackoverflow answers posted before and none of then are working for me.

How can I proceed? Thanks!

3

9 Answers

The code below should work:

string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Data\Names.txt"); string[] files = File.ReadAllLines(path); 
8

it depends where is your Data folder

To get the directory where the .exe file is:

AppDomain.CurrentDomain.BaseDirectory 

To get the current directory:

Environment.CurrentDirectory 

Then you can concatenate your directory path (@"\Data\Names.txt")

If you need to get all the files in the folder named 'Data', just code it as below

string[] Documents = System.IO.Directory.GetFiles("../../Data/"); 

Now the 'Documents' consists of array of complete object name of two text files in the 'Data' folder 'Data'.

1

I have a C# project (Windows Console Application). I have created a folder named Images inside project. There is one ico file called MyIcon.ico. I accessed MyIcon.ico inside Images folder like below.

this.Icon = new Icon(@"../../Images/MyIcon.ico"); 
0

Copy Always to output directory is set then try the following:

 Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); String Root = Directory.GetCurrentDirectory(); 
3

This was helpful for me, if you use the

var dir = Directory.GetCurrentDirectory() 

the path fill be beyond the current folder, it will incluide this path \bin\debug What I recommend you, is that you can use the

string dir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName 

then print the dir value and verify the path is giving you

For Xamarin.iOS you can use the following code to get contents of the file if file exists.

var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); var filename = Path.Combine(documents, "xyz.json"); if (File.Exists(filename)) { var text =System.IO.File.ReadAllText(filename); } 

Use this Code for read all files in folder and sub-folders also

class Program { static void Main(string[] args) { getfiles get = new getfiles(); List<string> files = get.GetAllFiles(@"D:\Document"); foreach(string f in files) { Console.WriteLine(f); } Console.Read(); } } class getfiles { public List<string> GetAllFiles(string sDirt) { List<string> files = new List<string>(); try { foreach (string file in Directory.GetFiles(sDirt)) { files.Add(file); } foreach (string fl in Directory.GetDirectories(sDirt)) { files.AddRange(GetAllFiles(fl)); } } catch (Exception ex) { Console.WriteLine(ex.Message); } return files; } } 
string myFile= File.ReadAllLines(Application.StartupPath.ToString() + @"..\..\..\Data\myTxtFile.txt") 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.