C
C#3y ago
daveeska

❔ Failed reading a file

The code: class Server { private string usrcLoc; private int totalUsrs; private void init() { string usercount = usrcLoc; if (File.Exists(usercount)) { totalUsrs = int.Parse(File.ReadAllText(usercount)); } else { Console.WriteLine("oof"); totalUsrs = 0; } } public Server() { usrcLoc = @"C:\temp\ChaTe\ChatList\usrcount.txt"; totalUsrs = 0; }
34 Replies
daveeska
daveeskaOP3y ago
it says "oof" which mean that it failed reading the file
daveeska
daveeskaOP3y ago
but the file exist
circles.png
circles.png3y ago
does the file exist? o try forward slashes
daveeska
daveeskaOP3y ago
nope didnt work
Pobiega
Pobiega3y ago
and if you open the file? is there a valid number in it?
daveeska
daveeskaOP3y ago
doesnt windows uses \? yes
Pobiega
Pobiega3y ago
C# supports / just fine. its more related to escape
daveeska
daveeskaOP3y ago
Pobiega
Pobiega3y ago
no spaces before, after? newlines?
daveeska
daveeskaOP3y ago
nope
circles.png
circles.png3y ago
permissions are alright?
daveeska
daveeskaOP3y ago
hmm? how do i check?
circles.png
circles.png3y ago
cause its in c: which i dont believe has sufficient permissions used to use windows but right click and then properties
daveeska
daveeskaOP3y ago
then?
circles.png
circles.png3y ago
security i think
daveeska
daveeskaOP3y ago
k
circles.png
circles.png3y ago
just for that file make it read and writable by everyone or your user
daveeska
daveeskaOP3y ago
is this okay?
Pobiega
Pobiega3y ago
given taht code thou, the "File.Exists" call is failing. which is curious.
daveeska
daveeskaOP3y ago
but the file do exist
circles.png
circles.png3y ago
how about users?
Pobiega
Pobiega3y ago
try C:/temp/ChaTe/ChatList/usrcount.txt
daveeska
daveeskaOP3y ago
nope, didnt work worked*
deluvas
deluvas3y ago
C:/temp/ChatTe/ChatList/usrcount.txt there was a t missing in ChatTe
circles.png
circles.png3y ago
maybe
Pobiega
Pobiega3y ago
yep, thats it
daveeska
daveeskaOP3y ago
oh right how am i so dumb lol thx btw
deluvas
deluvas3y ago
if you wanna use the directory from where the program was started (aka the working directory) you can use usrcLoc = Path.Combine(Directory.GetCurrentDirectory, "usrcount.txt") or if you want to make it based on the location of the program itself you can use usrcLoc = Path.Combine(GetType().Assembly.Location, "usrcount.txt") btw depending on how you wanna use the program this could make things easier for you then hardcoding the path
daveeska
daveeskaOP3y ago
okay, thanks ^^
ero
ero3y ago
work*
daveeska
daveeskaOP3y ago
yea
Buddy
Buddy3y ago
You should use AppContext.BaseDirectory
Accord
Accord3y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?