Please can somebody help me with this .txt file?
I am trying to read all text from a .txt file called dna.txt, it currently contains one line of text, however for some reason File.ReadAllText is returning an empty string?
10 Replies
I'm afraid there's not much we can do to help. Yes, the 2 lines of code you're posted should do what you describe
Maybe you've got 2 files called dna.txt in different folders, and it's not reading the one you expect?
How are you running your project? What file will be read depends on how you run your project and from where
I am running it on Visual Studio 2022 on my windows PC using the system IO library
right, and you have verified that this does not crash when you run it?
yes I am able to get other console writeline outputs and there are no errors, when i try print the file string i get an empty line outputted
Can you try call
Directory.GetCurrentDirectory()
and print that? Then copy that path to File Explorer and see if it shows the path to the expected dna.txt
or notor try to get the file by it's absolute path instead of the relative one
Try writing it like this
The @ is important cause you're dealing with file location
The
@
in this example does absolutely nothing
Where is this file in relation to your project?
You probably want to set it as "copy always" or "copy if newer" in file properties. Right-click it in the solution explorer in VS, open properties, and change how it's copiedThat's no different to what OP is doing, except using more lines