❔ 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
it says "oof" which mean that it failed reading the file
but the file exist
does the file exist?
o
try forward slashes
nope
didnt work
and if you open the file? is there a valid number in it?
doesnt windows uses
\
?
yesC# supports / just fine. its more related to escape
no spaces before, after? newlines?
nope
permissions are alright?
hmm?
how do i check?
cause its in
c:
which i dont believe has sufficient permissions
used to use windows but right click and then propertiesthen?
security i think
k
just for that file make it read and writable by everyone
or your user
is this okay?
given taht code thou, the "File.Exists" call is failing.
which is curious.
but the file do exist
how about users?
File.Exists(String) Method (System.IO)
Determines whether the specified file exists.
try
C:/temp/ChaTe/ChatList/usrcount.txt
nope, didnt work
worked*
C:/temp/ChatTe/ChatList/usrcount.txt
there was a t missing in ChatTemaybe
yep, thats it
oh right
how am i so dumb lol
thx btw
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 pathokay, thanks ^^
work*
yea
You should use
AppContext.BaseDirectory
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.