C#
C#

help

Root Question Message

minjub
minjub3/10/2023
❔ 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; }
minjub
minjub3/10/2023
it says "oof" which mean that it failed reading the file
minjub
minjub3/10/2023
but the file exist
circles.png
circles.png3/10/2023
does the file exist?
circles.png
circles.png3/10/2023
o
circles.png
circles.png3/10/2023
try forward slashes
minjub
minjub3/10/2023
nope
minjub
minjub3/10/2023
didnt work
Pobiega
Pobiega3/10/2023
and if you open the file? is there a valid number in it?
minjub
minjub3/10/2023
doesnt windows uses \?
minjub
minjub3/10/2023
yes
Pobiega
Pobiega3/10/2023
C# supports / just fine. its more related to escape
minjub
minjub3/10/2023
Pobiega
Pobiega3/10/2023
no spaces before, after? newlines?
minjub
minjub3/10/2023
nope
circles.png
circles.png3/10/2023
permissions are alright?
minjub
minjub3/10/2023
hmm?
minjub
minjub3/10/2023
how do i check?
circles.png
circles.png3/10/2023
cause its in c: which i dont believe has sufficient permissions
circles.png
circles.png3/10/2023
used to use windows but right click and then properties
minjub
minjub3/10/2023
then?
circles.png
circles.png3/10/2023
security i think
minjub
minjub3/10/2023
k
circles.png
circles.png3/10/2023
just for that file make it read and writable by everyone
circles.png
circles.png3/10/2023
or your user
minjub
minjub3/10/2023
is this okay?
Pobiega
Pobiega3/10/2023
given taht code thou, the "File.Exists" call is failing.
Pobiega
Pobiega3/10/2023
which is curious.
minjub
minjub3/10/2023
but the file do exist
circles.png
circles.png3/10/2023
how about users?
Pobiega
Pobiega3/10/2023
try C:/temp/ChaTe/ChatList/usrcount.txt
minjub
minjub3/10/2023
nope, didnt work
minjub
minjub3/10/2023
worked*
deluvas1911
deluvas19113/10/2023
C:/temp/ChatTe/ChatList/usrcount.txt
there was a t missing in ChatTe
circles.png
circles.png3/10/2023
maybe
Pobiega
Pobiega3/10/2023
yep, thats it
minjub
minjub3/10/2023
oh right
minjub
minjub3/10/2023
how am i so dumb lol
minjub
minjub3/10/2023
thx btw
deluvas1911
deluvas19113/10/2023
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
minjub
minjub3/10/2023
okay, thanks ^^
Ero
Ero3/10/2023
work*
minjub
minjub3/10/2023
yea
Buddy
Buddy3/10/2023
You should use AppContext.BaseDirectory
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy