C
C#2y 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
daveeskaOP2y ago
it says "oof" which mean that it failed reading the file
daveeska
daveeskaOP2y ago
but the file exist
circles.png
circles.png2y ago
does the file exist? o try forward slashes
daveeska
daveeskaOP2y ago
nope didnt work
Pobiega
Pobiega2y ago
and if you open the file? is there a valid number in it?
daveeska
daveeskaOP2y ago
doesnt windows uses \? yes
Pobiega
Pobiega2y ago
C# supports / just fine. its more related to escape
daveeska
daveeskaOP2y ago
Pobiega
Pobiega2y ago
no spaces before, after? newlines?
daveeska
daveeskaOP2y ago
nope
circles.png
circles.png2y ago
permissions are alright?
daveeska
daveeskaOP2y ago
hmm? how do i check?
circles.png
circles.png2y ago
cause its in c: which i dont believe has sufficient permissions used to use windows but right click and then properties
daveeska
daveeskaOP2y ago
then?
circles.png
circles.png2y ago
security i think
daveeska
daveeskaOP2y ago
k
circles.png
circles.png2y ago
just for that file make it read and writable by everyone or your user
daveeska
daveeskaOP2y ago
is this okay?
Pobiega
Pobiega2y ago
given taht code thou, the "File.Exists" call is failing. which is curious.
daveeska
daveeskaOP2y ago
but the file do exist
circles.png
circles.png2y ago
how about users?
Pobiega
Pobiega2y ago
try C:/temp/ChaTe/ChatList/usrcount.txt
daveeska
daveeskaOP2y ago
nope, didnt work worked*
deluvas
deluvas2y ago
C:/temp/ChatTe/ChatList/usrcount.txt there was a t missing in ChatTe
circles.png
circles.png2y ago
maybe
Pobiega
Pobiega2y ago
yep, thats it
daveeska
daveeskaOP2y ago
oh right how am i so dumb lol thx btw
deluvas
deluvas2y 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
daveeskaOP2y ago
okay, thanks ^^
ero
ero2y ago
work*
daveeska
daveeskaOP2y ago
yea
Buddy
Buddy2y ago
You should use AppContext.BaseDirectory
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server