Programming Languages Problems with running on Local visual studio

For the last Programming Languages project for Learn C#

Hi I have completed this project in the codecademy platform. But when I copy it over to my local Visual Studio it wont work complaining that the languages.tsv cannot be found.

Especially at this line

            List<Language> languages = File.ReadAllLines("./languages.tsv") // here as far as I am concered they are both at the same level
              .Select(line => Language.FromTsv(line))

The tsv file is in the same directory level as Program.cs where I am doing that method for File.ReadLine() but still it wont work in my local enviroment but it works in codecademy platform

How can I make it work for my local environment?

As this is the warning:

System.IO.FileNotFoundException Message=Could not find file ‘C:\Users\MYPC\Codecademy\C#\Learn-C#\9.List-And-LINQ\1.Programming-Languages\ProgrammingLanguages\bin\Debug\net6.0\languages.tsv’.

But as far as I know I had placed the language.tsv inside the ProgrammingLanguages directory where Program.cs lives. So I dont know why its looking for it bin etc.

As I am telling it look for it inside the same level as this file as I understand File.ReadAllLines("./languages.tsv") so → "./languages.tsv" is basically saying in this same directory same path level.

So why is it looking somewhere else?

Here are two images of where my two files lives

Then inside it

Without seeing all your code…

  1. It would be easier for you if you were to place your file into another directory, then assign the working directory in your code to look for your file.

  2. You probably don’t want that file in the same location as where your code is running at:

Environment.CurrentDirectory = @"C:\<DirectoryName>"; List<Language> languages = File.ReadAllLines(@".\languages.tsv") // here as far as I am concered they are both at the same level .Skip(1) .Select(line => Language.FromTsv(line)) .ToList();

The problem is, where your .cs files are and where the complied files get put are not the same place. What you need to do is in visual studio add the missing file to the project, then click on the file in the solution explorer, then there is an option called Copy to Output Directory under properties. This is going to currently be set to do not copy, change it to be copy always or copy if newer - doesn’t really matter for your use case. This will add some stuff to the project file telling it to include the file and where to put it etc, you could edit the project file directly but no need here as it’s easy enough to do in the GUI.

1 Like