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?
No description
10 Replies
canton7
canton75d ago
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?
Thinker
Thinker5d ago
How are you running your project? What file will be read depends on how you run your project and from where
SlimeShulker
SlimeShulkerOP5d ago
I am running it on Visual Studio 2022 on my windows PC using the system IO library
Thinker
Thinker5d ago
right, and you have verified that this does not crash when you run it?
SlimeShulker
SlimeShulkerOP5d ago
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
Get
Get5d ago
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 not
MisterOrange
MisterOrange4d ago
or try to get the file by it's absolute path instead of the relative one
Helix W.
Helix W.4d ago
Try writing it like this
var bytes = File.ReadAllBytes(@"dna.txt");
var bytes = File.ReadAllBytes(@"dna.txt");
var text = Encoding.UTF8.GetString(bytes);
var text = Encoding.UTF8.GetString(bytes);
Console.WriteLine(text);
Console.WriteLine(text);
The @ is important cause you're dealing with file location
Angius
Angius4d ago
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 copied
canton7
canton74d ago
That's no different to what OP is doing, except using more lines

Did you find this page helpful?