'Adding a CSV file to a project in Visual Studio

I am working on a project where I have to read in serveral pre-existing CSV (dog.csv, horse.csv, etc.). I want to know how would I add these file into my project so that I may test to see if my print functions work (the code is written in c++). Would I have to copy and paste the files into the debugging folder or would I place it under the test folder of the project?



Solution 1:[1]

You can include the files in your project in whatever (sub)folder you wish by using Right click -> Add -> Existing Item. Then, right-click on each file and choose Properties. Set up "Copy to output directory" to "Copy if newer".

Then after build, your files will be copied into the bin/debug folder.

To read the file, you can just use:

System.IO.File.ReadAllText("dog.csv");

Solution 2:[2]

This link should help guide you how to add CSV files to a project.

If you wanted to do a down and dirty way you could just save the CSV's somewhere on your local machine, and then hard code the file path to that location.

Example:

c:\test\Dog.csv and then set that as a variable for whenever you need to read in the csv file.

Solution 3:[3]

Another possible way is to add a file within project, right click and select properties, and then in Copy to Output Directory, select Copy always. This way, csv file will be automatically copied in your debug and release packages too.

string executableLocation = Path.GetDirectoryName(
    Assembly.GetExecutingAssembly().Location);
string csvLocation= Path.Combine(executableLocation, "file.csv");

Above code will read file location from bin directory where your csv file will be stored.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Josie G. Bigler
Solution 3