-1

I did right click on my project then Add > Existing Item and then selected the text file from the hard disk. Now i see the file on the right window in the solution explorer.

Now i want to do two things. First to make sure if someone else will use the project or only will run the exe or when i will make later installation that the program will be able to use the text file not only me. I mean that if someone else on his computer will use the program that it will not throw exception that the text file is missing.

Second how can i read from the file in the code ?

private void ManageDirectories()
        {
            string savedImagesPath = Path.GetDirectoryName(Application.LocalUserAppDataPath);
            string mainPath = "Test";
            mainPath = Path.Combine(savedImagesPath, mainPath);
            //string[] lines = File.ReadAllLines
        }

I want to read the lines of the text file using the File.ReadAllLines

Abdul Jaja
  • 85
  • 8
  • As to your first question, Mark the file in the solution explorer, and then, in the properties window, change the value of Copy to Output Directory to `Copy if newer` – Zohar Peled Jan 26 '17 at 12:55

1 Answers1

1

If you want this file to exist in Debug/Release folder with .exe file you should set Copy to Output Directory option to true.

To do this:

Click on file -> in Properties window set Copy to Output Directory to true.

To read all lines from some file you can use File.ReadAllLines():

string[] lines = File.ReadAllLines("somefile.txt"); //change path and name of your file.
Roman
  • 11,966
  • 10
  • 38
  • 47