Reputation: 83
I'm new here at this forum. I have a C# Problem with System.IO.File.ReadAllBytes()
. I read a filepath from a textbox like this C:\Users\Janek\Pictures\testpicture.
spath = tb_path.text;
System.IO.File.ReadAllBytes(spath);
But that doesnt work. VisualStudio says that's a wrong path format. How can iI build a working path?.
spath = "@" + tb_path.text;
System.IO.File.ReadAllBytes(path);
Didn't worked for me. It is certainly easy, but no matter what I try it does not work. But I think someone can help me here. Thanks for help!
Upvotes: 0
Views: 24571
Reputation: 1608
Your code appears OK. The problem can be in the data you enter into the textbox instead. "C:\Users\Janek\Pictures\testpicture" can be a valid file name. but most likely it isn't, and you are missing a file extension. Something like
C:\Users\Janek\Pictures\testpicture.jpg
or
C:\Users\Janek\Pictures\testpicture.bmp
or
C:\Users\Janek\Pictures\testpicture.png
or whatever you file type is, is more likely to be correct.
Upvotes: 1
Reputation: 682
Don't add "@" to the string, add it on the outside?
string path = tb_path.Text;
var x = File.ReadAllBytes(@path);
You want to escape like this : (@"C:\") not ("@C:\")
Upvotes: 2